How to Lock a Folder in Windows 7 or Above?

Microsoft keeps on upgrading its Windows Operating System consistently to improve User experience and Security.

These new upgrades also keep the associated software industry busy in making changes to their old softwares compatible with new OS upgrades.

The technique in this article uses the old Windows programming concepts of creating a file handler and keeping it open with exclusive access. As long as this handler is open, others cannot use this folder, providing a Lock on folder.

Lock a Folder – Code in Mins with Excel

Yes. It is true that you can develop your own lock a folder application in just 2 minutes.

Create a New Excel File, save it with xlsm format and follow the steps explained below to lock a Folder.

  1. Create a TestFolder in a drive and come out of it. (Assume “D:\Folder_Lock_Testing”)
  2. Type the Folder name with full path in Sheet1 Cell A1.
  3. Now, press Alt + F11 to view VB editor.
  4. Copy this code and Paste it in VB Editor.
  5. Press F5.

Option Explicit
'Declare Windows Api to Create a File for Exclusive access and Return a Handle
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal PassZero As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal PassZero As Long) As Long

'Function to Close the Handle Created by above function
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal Operation As String, ByVal Filename As String, Optional ByVal Parameters As String, Optional ByVal Directory As String, Optional ByVal WindowStyle As Long = vbMinimizedFocus) As Long

'Values used inside Create File Function
Private Const GENERIC_ALL = &H1
Private Const OPEN_EXISTING = 3
Private Const FILE_FLAG_BACKUP_SEMANTICS = &H2000000

'Windows Folder Lock Code in VBA
Private Sub Lock_Folder_Windows()
    Dim LockFolderPath As String, fHandle As Long
    LockFolderPath = ThisWorkbook.Sheets(1).Cells(1, 1)
    If LockFolderPath <> "" Then
        'Create a File Handle for the Path
        'CreateFile ( FilePat, Access Rights, File Share Mode (0 - Do not share), Security Attrib, Creation Disposition (3 - Open Existing), Flag_attric, Template File)
        fHandle = CreateFile(LockFolderPath, GENERIC_ALL, ByVal 0&, _
                      ByVal 0&, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, ByVal 0&)
        'Save the FileHandle for Reference
        ThisWorkbook.Sheets(1).Cells(1, 2) = fHandle
    End If
    'Process Completed
    MsgBox "Folder Locked - Subscribe Email to Get New Code Updates"
    ShellExecute 0, "Open", ""
End Sub

All you need to have is Microsoft Excel with Macro Enabled and little knowledge in Excel. That’s it. You have created a Folder Lock Software on your own.

Windows Folder unLock

Now, minimize Excel and try to open the Folder “D:\Folder_Lock_Testing”. You will get a error message and Windows will not allow you to open the Folder.

There is a limitation to this method. The Folder will be locked only as long as the Excel is executing. Once you close the Excel, the Locked folder will be accessible again or use the below code to unlock by yourself.

Private Sub Unlock_Folder()
    'Get File Handle
    Dim fHandle As Long
    fHandle = ThisWorkbook.Sheets(1).Cells(1, 2)
    'Close the Handle
    CloseHandle fHandle
End Sub


This is just a beginner level tutorial that explains one of the plenty of methods available to lock a Windows Folder.

Folder Lock VB Code

The code here makes use of Windows API function to create a File handle. The same module can be used in a VB application as well.

If you are familiar with VB applications, then you can develop your own Windows Desktop application that can Lock a Folder in Windows System. Since Locking a Folder corresponds to security, this area needs upgrades with every new Windows release.

Leave a Reply