Lock Folder Using Command Prompt

59

Solution 1

To Lock a folder or file,

cacls "YOURPATH" /E /P everyone:N

To UnLock a folder or file,

cacls "YOURPATH" /E /P everyone:F

Note: Do not lock C:\ OR Operating System drive using this trick otherwise Windows will not boot next time.

Solution 2

To achieve actual password protection for your files and folders, you can use compressing software from command line to make a password protected archive. One such third party tool is 7-zip (its open source and very popular).

With this method you can use the inbuilt Command line interface (cmd) to Securely Lock and Unlock files/folders.

First you need to download the 7-Zip command line version (here), named 7za.exe. This is the exe you will use to run commands on archives.
For convenience and so you don't need to change environment paths, put the 7za.exe file in your user directory.

Example:

7za a archive.7z -psecret -mhe subdir\
adds all files and subfolders from folder subdir to archive archive.7z using password "secret". Also it encrypts archive headers (-mhe switch), so filenames will be encrypted.

7za x archive.7z -psecret
extracts all files from archive.zip using password "secret".

Elaborate explanation of using 7zip from command line is here.

Note:
Remember to Delete the original Folder after archieving it.
The drawback is that every time you need access to that folder you have to extract it first. This can be automated by writing a bat file for the purpose of locking and unlocking.

Solution 3

That's not possible....there is no way to password protect a folder without a third-party software.
The best you can do is encrypt it using windows default features. But technically it wont be password protected...

Solution 4

The normal way on windows to protect files (in a folder) would be EFS. However it is tied to the login password, so you don't really see its effect (and it does not help if you share the password or the account).

Real password protection for a selected directory tree can be achieved with disk encryption tools (like truecrypt). When you want to unlock the directory, you need to mount the truecrypt file, and for this you need the password. BitLocker is the build in option from Windows, but I think you need the Ultimate edition to use it as flexible as TrueCrypt in this scenario.

Solution 5

This is a very simple trick that can hide the folder. But not password protecting it. Anyone who has basic knowledge in batch programming can write batch file to unhide it. But you can make it into exe file and then nobody can change the password as in batch file. But they can write another batch file to do it. This kind of folder is not visible even when chosen the show hidden file option. Compile it into exe file.

if NOT EXIST Locker goto MDLOCKER
:CONFIRM
echo Are you sure u want to Lock the folder(Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto END
if %cho%==N goto END
echo Invalid choice.
goto CONFIRM
:LOCK
ren Locker "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto End
:UNLOCK
echo Enter password to Unlock folder
set/p "pass=>"
if NOT %pass%==type your password here goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Locker
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:MDLOCKER
md Locker
echo Locker created successfully
goto End
:End
Share:
59

Related videos on Youtube

Thomas Landgraf
Author by

Thomas Landgraf

I found love about programming early in my life and ever since coding almost everyday. Coding keeps me up in the night. I love writing software that solves real-world problems. Strongly passionate about technology, software and mobile apps.

Updated on September 18, 2022

Comments

  • Thomas Landgraf
    Thomas Landgraf over 1 year

    When the following code runs, it doesn't complete in its entirety. I usually have to run it a few more times to ensure all the data in that range has been inspected and the row deleted if it meets my criteria.

    Const A% = 1
    Const B% = 2
    Const C% = 3
    Const D% = 4
    
    'Some code
    
    If myCL <> "" Then
        For Each Cell In RngB.Cells
            If Cell.Value <= myBal Then
                r = Cell.Row
                If ws.Cells(r, D) <= myScore And ws.Cells(r, C) Like myCL Then
                    Cell.EntireRow.Delete
                End If
            End If
        Next Cell
    ElseIf myCL = "" Then
        For Each Cell In RngB.Cells
            If Cell.Value <= myBal Then
                r = Cell.Row
                If ws.Cells(r, D) <= myScore Then
                    Cell.EntireRow.Delete
                End If
            End If
        Next Cell
    End If
    

    I understand that I should loop through a range in reverse when using something like For i = ## to 1 Step -1, but I don't believe this would apply in this situation.

    My issue is that when Cell should meet the criteria, it sometimes skips over it, which I then rerun the code and it will be deleted.

    • Ankit
      Ankit over 11 years
      Do you mean password protect it ?
    • Thomas Landgraf
      Thomas Landgraf over 11 years
      @Lamb: yes, exactly.
    • Ankit
      Ankit over 11 years
      Does it needs to be hidden ?
    • Thomas Landgraf
      Thomas Landgraf over 11 years
      @Lamb: No,it shouldn't be hidden. It should be password protected
    • Canadian Luke
      Canadian Luke over 11 years
      Using MS-DOS, you can't without a third party program
    • Thomas Landgraf
      Thomas Landgraf over 11 years
      We can do it.I Forgot the way how i did it. But, i am pretty sure we can do it.
    • Canadian Luke
      Canadian Luke over 11 years
      @akira He said Command Prompt in the title, so I moved that into the body
    • Canadian Luke
      Canadian Luke over 11 years
      @akira Yes, I removed the ms-dos tag, but MS DOS would not support it; his title read Command Prompt, so I would assume it's for Windows 2000 or higher, but the OP still hasn't clarified himself. Once he does, he can edit it in himself or I can. We still don't know what the OS was, and from the original post we still aren't completely sure
    • Thomas Landgraf
      Thomas Landgraf over 11 years
      for windows xp OS
    • Ƭᴇcʜιᴇ007
      Ƭᴇcʜιᴇ007 over 11 years
  • Thomas Landgraf
    Thomas Landgraf over 11 years
    this is nothing but setting permissions. I need to set the password for the folder.
  • Thomas Landgraf
    Thomas Landgraf over 11 years
    thanks for the answer. But, the thing is, if we use software it's the simplest way to lock the folder. But, what i need is password protected should be done using only command line and without using any softwares. And, thanks for the help.
  • tumchaaditya
    tumchaaditya over 11 years
    that's not possible....there is no way to password protect a folder without a third-party software. The best you can do is encrypt it..
  • Ankit
    Ankit over 11 years
    @tumchaaditya What do you mean by that's not possible, Does it not works ? Did you read the answer? it mentions that it relies on 3rd party tool. But it still satisfy the requirement -"How can we lock folder using the Command Prompt". It is not mentioned in the question that 3rd party tool should not be involved, actually OP wants to do it from cmd. correct me if I'm wrong.
  • tumchaaditya
    tumchaaditya over 11 years
    1. i specfically menetioned "without using third party software" and 2. if you read carefully, the person asking question specifically mentions avoiding third party tools in comment on this answer(and i was targeting this requirement when I said "that's not possible")....pardon...should have included @ in comment...
  • Ankit
    Ankit over 11 years
    @tumchaaditya extremely sorry, Its my fault misinterpreted the comment.
  • Ankit
    Ankit over 11 years
    @VenkatManohar It will work from command line itself, you do not need to use the GUI of the Software. Just one condition - you have to have that 3rd party exe in your computer (but it will work from cmd, no direct interaction with the software is required)
  • Thomas Landgraf
    Thomas Landgraf over 11 years
    @Lamb: without any 3rd party exe in my computer can we do that or not
  • Ankit
    Ankit over 11 years
    It appears that we can't. :(
  • Excelosaurus
    Excelosaurus over 6 years
    This will work fine if there aren't too many rows to delete; Union's performance degrades geometrically when called in a loop.