# Batch script to make folders hidden

I just wrote a simple script cmd to hide private folders by combining several scripts from the Internet. Can anyone give me suggestions for improvement?

This batch file uses a password hidden by powershell script. Then I create a shortcut and run attrib -s -h, so it is not easy to find. It works just gave attrib -s -h on the desired folder. Location folder can only be changed by editing the file. Also passwords can only be changed by editing the batch file. The last two things are quite cumbersome.

@echo off
title Privat Locker
set _folder="E:\Private"

dir /a:h %_folder%>nul 2>nul
if %errorlevel%==0 goto PASS1

dir /a:s %_folder%>nul 2>nul
if %errorlevel%==0 goto PASS1

:CONFIRM
echo Do you want to lock the Private 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 Error! Wrong answer, please type [Y/N]
goto CONFIRM

:LOCK
attrib +h +s %_folder%
echo The folder sucsessfully locked...
pause
goto end

:PASS1
set "psCommand=powershell -Command "$pword = read-host 'Masukkan PIN!' -AsSecureString ; ^$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^ [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (%psCommand%) do set password=%%p
if NOT "%password%"=="YOURPINHERE" goto FAIL1
attrib -s -h %_folder%
goto EXP

:PASS2
set "psCommand=powershell -Command "$pword = read-host 'Masukkan PIN!' -AsSecureString ; ^$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^ [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (%psCommand%) do set password=%%p
if NOT "%password%"=="YOURPINHERE" goto FAIL2
attrib -s -h %_folder%
goto EXP

:PASS3
set "psCommand=powershell -Command "$pword = read-host 'Masukkan PIN!' -AsSecureString ; ^$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^ [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (%psCommand%) do set password=%%p
if NOT "%password%"=="YOURPINHERE" goto FAIL3
attrib -s -h %_folder%

:EXP
echo Folder successfully unlocked, do you want to look it? [Y/N]
set/p "tuju=>"
if %tuju%==Y goto BUKA
if %tuju%==y goto BUKA
if %tuju%==n goto end
if %tuju%==N goto end
echo Error! Pilihan salah, ketik [Y/N]
goto EXP

:BUKA
echo Go to folder...
pause
start explorer %_folder%
goto end

:FAIL1
echo Wrong PIN!
echo Try again, 2 remaining
goto PASS2

:FAIL2
echo Wrong PIN!
echo Try again! 1 remaining
goto PASS3

:FAIL3
echo Wrong PIN!
echo Device will reboot!

:PIL
set/p "rst=Automatic Restart? [Y/N]"
if %rst%==Y goto RST
if %rst%==y goto RST
if %rst%==N goto MNL
if %rst%==n goto MNL
echo Error! wrong choice, pleas type [Y/N]
goto PIL

:RST
shutdown -r -t 15 -c "Computer will reboot on 15 second's"
pause
goto end

:MNL
echo If device not rebooting on 15 minutes, all data[s] in the folder
echo will be erased...
pause

:end


## 1 Answer

I am glad you asked your question here. I'm hoping to explain why what you are trying to do is a bad idea. I cannot speak much for improving your batch as that is not my strong point but that should not matter as the focus of the review is ....

# Security Through Obscurity

If you are unfamiliar with that phrase as a concept Wikipedia has a good primer on it.

If I understand the script correctly it is designed to mark a folder hidden. If it is already hidden then using a password will unlock it. You have a very flawed approach to security as it would only keep the most naive of users in the dark. The following is a combination of points, in no particular order (since the really refer to each other), that illustrate why you should not bother doing this.

Hiding a folder does not prevent access to it. A simple control panel change would expose those folders and since the security of the folder has not changed a user can just walk right in. Or if you knew the full path then you can just type it in Windows Explorer or the Run menu.

The last two things are quite cumbersome.

You have the "password" hard-coded in the script. It is a very trivial action to make changes. Again this is only keeping a certain breed of user from accessing your document location. Since neither the script or folder are technically secure then it is moot.

PowerShell code is doing nothing but masking the view. You take in a secure password but immediately unsecured it. The only thing that approach would prevent is someone directly behind you watching what you are doing and not looking directly at the keyboard.

# Consider PowerShell

For reasons stated above I cannot condone this code but there are some concepts you should be aware of that PowerShell can do better. While you code is mostly batch you have a splash of PowerShell there so I think you have at least a vague familiarity with it.

Use functions

As I am sure you know there are several instances of repeated code. Those could be easily replaced with some simple functions that you could call in a loop that runs 3 times for example.

A much improved choice menu

PowerShell has a great way of creating menus guiding user input and acting on the results. It is not too hard to get a grasp at first glance so I am going to include the code snippet from the from the front of that article.

$title = "Delete Files"$message = "Do you want to delete the remaining files in the folder?"

$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",  "Deletes all the files in the folder."$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",
"Retains all the files in the folder."

$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)$result = $host.ui.PromptForChoice($title, $message,$options, 0)

switch (\$result)
{
0 {"You selected Yes."}
1 {"You selected No."}
}


# Consider Encryption

If you were still concerned about the safety and security of your documents where multiple users would have access consider an encryption solution. It's too much to cover here but there are several simple approaches involving encryption software (I cannot recommend any specific one.).

• "A simple control panel change would expose those folders" Indeed, this is one of the first things I change on nearly every Windows machine I have a log in for. It is not at all uncommon for me to find myself rooting around in the hidden AppData directory, and I find it quite helpful to be aware of hidden files and folders from time to time. I imagine I am far from the only person who does this. I don't believe changing this setting even requires administrative privileges, since it is a per-user setting. Apr 17, 2016 at 8:57