# Solved: Batch file to hide/unhide a folder



## ellisbodds

Hi,

I'd like to create a batch file which allows me to hide or unhide a folder based on whether or not it is already hidden or not. Basically, if the folder is hidden, running the batch file would unhide it and if it was not hidden running the batch would hide it.

With some help from this thread I managed to get it working to show folders if they are hidden, but not vice versa. This is the script I am using:


Code:


For /F "Delims=" %%I In ('Attrib Locker^|Findstr /BR "....H"') Do (
Attrib -H Locker
Attrib /D /S -H Locker\*.*
)

If you could post the code I'd need to use to get both working in one batch file that would be superb, assume the folder is called "Locker" and is in the local directory.

Thanks


----------



## TheOutcaste

Welcome to TSG!

Easier to use the If statement in this case:


Code:


Set _Path=C:\Locker
For /F "Delims=" %%I In ('Attrib "%_Path%"') Do Set _Attribs=%%I
If "%_Attribs:~4,1%"=="H" (
  Attrib -H "%_Path%"
  Attrib /D /S -H "%_Path%"\*.*
) Else (
  Attrib +H "%_Path%"
  Attrib /D /S +H "%_Path%"\*.*
)


----------



## Squashman

This is pretty trivial when the user just flips on show hidden files/folders in Explorers settings. Then your batch file becomes useless.


----------



## ellisbodds

TheOutcaste said:


> Welcome to TSG!
> 
> Easier to use the If statement in this case:
> 
> 
> Code:
> 
> 
> Set _Path=C:\Locker
> For /F "Delims=" %%I In ('Attrib "%_Path%"') Do Set _Attribs=%%I
> If "%_Attribs:~4,1%"=="H" (
> Attrib -H "%_Path%"
> Attrib /D /S -H "%_Path%"\*.*
> ) Else (
> Attrib +H "%_Path%"
> Attrib /D /S +H "%_Path%"\*.*
> )


Thanks 

It works great, but I forgot to mention that I want to make the file a system file as well :doh:

Any way you could include this?


Squashman said:


> This is pretty trivial when the user just flips on show hidden files/folders in Explorers settings. Then your batch file becomes useless.


See above 

In the context I'm going to be using this in, that will be enough protection.

EDIT: Got it working with this:


Code:


For /F "Delims=" %%I In ('Attrib Locker^') Do Set _Attribs=%%I
If "%_Attribs:~4,1%"=="H" (
Attrib -S -H Locker
Attrib /D /S -S -H Locker\*.*
) ELSE (
Attrib +S +H Locker
Attrib /D /S +S +H Locker\*.*
)

I didn't work it out, someone on Yahoo! Answers got there before you 

Thanks anyway.


----------



## TheOutcaste

Just add the +S and -S parameters to set or unset the system attribute.


Code:


Set _Path=C:\Locker
For /F "Delims=" %%I In ('Attrib "%_Path%"') Do Set _Attribs=%%I
If "%_Attribs:~4,1%"=="H" (
  Attrib -S -H "%_Path%"
  Attrib /D /S -S -H "%_Path%"\*.*
) Else (
  Attrib +S +H "%_Path%"
  Attrib /D /S +S +H "%_Path%"\*.*
)


----------



## Squashman

That still doesn't foil Windows Explorer.


----------



## ellisbodds

Squashman said:


> That still doesn't foil Windows Explorer.


I know, that's why I'm working on making it more secure 



Code:


@echo off

For /F "Delims=" %%I In ('Attrib Locker^') Do Set _Attribs=%%I
If "%_Attribs:~4,1%"=="H" (
goto password
) ELSE (
Attrib +S +H Locker
Attrib /D /S +S +H Locker\*.*
)
:end
exit

:password
cls
echo Please enter the password and press return:

set/p password=
if %password%==passwordhere goto YES
if not %password%==passwordhere goto NO

:YES
cls
echo That is the correct password.
pause
goto unlock

:NO
cls
echo The password you have entered is incorrect.
pause
goto end

:unlock
Attrib -S -H Locker
Attrib /D /S -S -H Locker\*.*

Now I'm going to convert this to an exe and save a copy of the batch script somewhere secure so I can edit it later if needs be.

Is there any way to make it so that when you enter the characters for the password they are replaced with a *, or not shown at all?


----------



## Squashman

The only way you are going to make it more secure it to encrypt it. Security through obscurity isn't security.


----------



## TheOutcaste

ellisbodds said:


> Now I'm going to convert this to an exe and save a copy of the batch script somewhere secure so I can edit it later if needs be.
> 
> Is there any way to make it so that when you enter the characters for the password they are replaced with a *, or not shown at all?


You can't hide the password in batch without a third party utility of some kind. You can do it in VBScript, which you can run from the batch file. It requires a couple other files, or you can create them on the fly.

Are you also denying access to the Folder Options Control Panel? Not much point in putting a password on this file when all anyone has to do is tell the system to show hidden and system files. You'd also want to block access to the Registry Editor, as it's pretty easy to change those settings by merging a registy file.


----------



## ellisbodds

Squashman said:


> The only way you are going to make it more secure it to encrypt it. Security through obscurity isn't security.





TheOutcaste said:


> You can't hide the password in batch without a third party utility of some kind. You can do it in VBScript, which you can run from the batch file. It requires a couple other files, or you can create them on the fly.
> 
> Are you also denying access to the Folder Options Control Panel? Not much point in putting a password on this file when all anyone has to do is tell the system to show hidden and system files. You'd also want to block access to the Registry Editor, as it's pretty easy to change those settings by merging a registy file.


To be honest guys, this folder doesn't need to be majorly secure, just out of view. When I saved the batch file elsewhere, I omitted the password. I just wanted to have the code for future reference 

I was under the impression that you could encrypt stuff in Windows with a password so that you would need to enter it every time you wanted access. It seems this only protects it when accessing from another computer. Once I realised that, I decided to make a batch file instead.


----------



## TheOutcaste

If you are using XP you can put the files in a Zip folder and add a password.
How to create and use compressed (zipped) folders in Windows XP

Vista and later you would need a third party program:
7-zip (Free - Open Source)
IZArc (Freeware)
WinAce (Free Trial)
WinRar (Free Trial)
WinZip (Free Trial)

If you are talking about Windows Encrypting File System, that allows access from your account without entering a password. Anyone logged in to a different account, or on a different computer will not be able to read the files without having the encryption key, but they will be able to see the file names (if they have Read access to the parent folder).

Third party encryption programs like TrueCrypt can be set to always require a password to mount the folder.

Here's the files for entering a hidden password. This doesn't work on my Win 7 x64 with IE8 for some reason, probably because Protected Mode is on.
Works on XP with IE6 and IE7, though you have to change it's settings to allow local active X objects to run, or you get the info bar prompt all the time.
You have to edit the path to the .htm file in the *password.vbs* file. Wouldn't be too hard to pass the current folder to it so it can find it. If the batch file creates the files when it's run you'd just insert the path.

*password.cmd*


Code:


@Echo Off
:: Gets a password using password.vbs script
For /F "Tokens=1,2 Delims==" %%I In ('CScript //nologo password.vbs') Do (
Set _UN=%%I
Set _PW=%%J
)
Echo username is %_UN%
Echo password is %_PW%

*password.vbs*


Code:


On Error Resume Next

Set objExplorer = WScript.CreateObject _
    ("InternetExplorer.Application", "IE_")

objExplorer.Navigate "file:///[COLOR=Red]C:\Test Dir\password.htm[/COLOR]"   
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 400
objExplorer.Height = 250 
objExplorer.Left = 300
objExplorer.Top = 200
objExplorer.Visible = 1             

Do While (objExplorer.Document.Body.All.OKClicked.Value = "")
    Wscript.Sleep 250                 
Loop 

strName = objExplorer.Document.Body.All.UserName.Value
strPassword = objExplorer.Document.Body.All.UserPassword.Value
strButton = objExplorer.Document.Body.All.OKClicked.Value
objExplorer.Quit
Wscript.Sleep 250

If strButton = "Cancelled" Then
    Wscript.Quit
Else
    set password = strPassword
    set Username = strName

   Wscript.Echo strName & "=" & strPassword
End If

*password.htm*


Code:


<SCRIPT LANGUAGE="VBScript">

Sub RunScript
    OKClicked.Value = "OK"
End Sub

Sub CancelScript
    OKClicked.Value = "Cancelled"
End Sub

</SCRIPT>

<HEAD>  
  
<TITLE>Network Credentials</TITLE>
<BODY>
<font size="2" face="Arial">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Enter your Network Username and Password</p>
Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font face="Arial">
<input type="name" name="UserName" size="40"></font></p>
<font size="2" face="Arial">
Password:&nbsp;&nbsp;&nbsp; </font><font face="Arial">
<input type="password" name="UserPassword" size="40"></font></p>

<input type="hidden" name="OKClicked" size = "20">

<input id=runbutton class="button" type="button" value=" OK " 
name="ok_button" onClick="RunScript">
&nbsp;&nbsp;&nbsp;
<input id=runbutton class="button" type="button" value="Cancel" 
name="cancel_button" onClick="CancelScript">

</BODY>


----------



## ellisbodds

Sorry, but I'm confused as to what that actually does


----------



## TheOutcaste

When you run the *password.cmd* file, it runs the *password.vbs* file to prompt you for a username and password. The password is masked (Screenshot1).
It uses IE to get this, which is what the *password.htm* file is for, it has the IE ActiveX script that gets the info and defines the size and shape of the window.
IE returns it to the *password.vbs* file, which then returns it to the batch file where it's assigned to the variables *_UN* and *_PW*.
This example batch file just echos them to the screen (screenshot2)


----------



## ellisbodds

So effectively I could use this to set a username and password for opening a folder?

Thanks, but I'm not sure I'll use it to be honest.


----------



## TheOutcaste

All that just lets you mask the password when you type it. If you don't need a username, you can remove that portion of the files.
All that so you can use the *input type="hidden"* feature, which is in the .htm file::


Code:


Password:&nbsp;&nbsp;&nbsp; </font><font face="Arial">
<input type="password" name="UserPassword" size="40"></font></p>

<[B][COLOR="Red"]input type="hidden"[/COLOR][/B] name="OKClicked" size = "20">


----------



## ellisbodds

TheOutcaste said:


> All that just lets you mask the password when you type it. If you don't need a username, you can remove that portion of the files.
> All that so you can use the *input type="hidden"* feature, which is in the .htm file::
> 
> 
> Code:
> 
> 
> Password:&nbsp;&nbsp;&nbsp; </font><font face="Arial">
> <input type="password" name="UserPassword" size="40"></font></p>
> 
> <[B][COLOR="Red"]input type="hidden"[/COLOR][/B] name="OKClicked" size = "20">


Thanks, I didn't realise you had to take it externally from the command prompt window in order to conceal your typing though


----------



## Squashman

TheOutcaste said:


> You can't hide the password in batch without a third party utility of some kind. You can do it in VBScript, which you can run from the batch file. .





ellisbodds said:


> Thanks, I didn't realise you had to take it externally from the command prompt window in order to conceal your typing though


That is basically what TheOutcaste was referring to in his previous posts.


----------



## TheOutcaste

If you are running XP Pro, you can use this:


Code:


@Echo Off
<Nul Set /P _PW=Password:
>GetPW.vbs Echo.wscript.echo CreateObject^("ScriptPW.Password").GetPassword^()
For /F "Tokens=* Delims=" %%I In ('CScript /nologo GetPW.vbs') Do Set _PW=%%I
Echo.
Echo.You entered %_PW%
Del GetPW.vbs

XP Home and Vista and later do not have the required DLL
Otherwise need a 3rd party tool
conset.exe (if you can find it)
editv32.exe (For 32 bit OS)
editv64.exe (For 64 bit OS) - http://www.westmesatech.com/editv.html

You can use Swiss File Knife to set the text color to match the background color (you should set the background color in the program first to be sure):


Code:


@Echo Off
Color 0B
Echo.Enter Password
sfk color black
Set _PW=
Set /P _PW=
sfk echo "[Yellow]You entered [Red]%_PW%"
sfk color grey
Goto:EOF


----------



## ellisbodds

TheOutcaste said:


> If you are running XP Pro, you can use this:
> 
> 
> Code:
> 
> 
> @Echo Off
> <Nul Set /P _PW=Password:
> >GetPW.vbs Echo.wscript.echo CreateObject^("ScriptPW.Password").GetPassword^()
> For /F "Tokens=* Delims=" %%I In ('CScript /nologo GetPW.vbs') Do Set _PW=%%I
> Echo.
> Echo.You entered %_PW%
> Del GetPW.vbs
> 
> XP Home and Vista and later do not have the required DLL
> Otherwise need a 3rd party tool
> conset.exe (if you can find it)
> editv32.exe (For 32 bit OS)
> editv64.exe (For 64 bit OS) - http://www.westmesatech.com/editv.html
> 
> You can use Swiss File Knife to set the text color to match the background color (you should set the background color in the program first to be sure):
> 
> 
> Code:
> 
> 
> @Echo Off
> Color 0B
> Echo.Enter Password
> sfk color black
> Set _PW=
> Set /P _PW=
> sfk echo "[Yellow]You entered [Red]%_PW%"
> sfk color grey
> Goto:EOF


To be honest, I think I am done with this batch no. It just needed to be a little bit secure (more secure than just a hidden folder) and that's what it is.

Thanks though, you've been a great help and I've finally got this working


----------



## TheOutcaste

You're Welcome!

If your issue has been resolved you (and ONLY you) can mark this thread Solved by using the Mark Solved button at the Top Left of this thread (above the first post) 










Jerry


----------



## ellisbodds

Will do, thanks


----------

