# Trying to hide a folder and unhide it in the same batch file with the attrib command

I am trying to hide a folder with a batch file, using the attrib command and I also want to unhide the file with the same batch file. can i do this as a single batch file? or do i have to call another and what commands do i use to make it possible. thanks

So far I have

attrib +h +s Foldername


and

attrib -h -s Foldername


But that isn't working because as soon as i unhide it, it hides again.

-
@SteveB I have updated and clarified the question, please help. Thanks. –  Daniel Apr 8 '12 at 8:40
Please don't post the same question twice, edit your previous question. –  Bob Apr 8 '12 at 11:27

I have tried this way:-

attrib +h +s tempFolder
pause
attrib -h -s tempFolder
pause


With this batch file, I can see the folder is hidding and unhidding accordingly.

-
I want it to detect if it is hidden or not and if it is hidden, unhide it and vice versa. I dont want to leave the command window up. I want it to complete its command and then when i want to, start the batch file again to do the opposite of what it just did. –  Daniel Apr 8 '12 at 11:16
@Daniel EDIT YOUR QUESTION. This is essential info that is missing from your original text. Look at the different answers you already got, they show that you are confusing people with your ambiguous question. –  Jan Doggen Sep 4 '14 at 13:35

Something like this should work:

Set _Path=C:\foldername
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%"\*.*
)


Got this from the techguys.org

-
Thanks, works perfectly! –  Daniel Apr 9 '12 at 1:31

Based on this comment, I assume you wish to create a batch file that will toggle the hidden state depending on the current state.

Based on your example, I assume you want to set it to both hidden and system if it is not hidden nor system at the moment, and if either one is set then clear both.

@echo off

set _folder="Foldername"

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

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

attrib +h +s %_folder%

exit /b

:clear
attrib -h -s %_folder%

exit /b


Ok, I'll explain this step by step.

• @echo off prevents output from showing up

• Using the _folder variable makes it easier to change the name. You could also set it to %*, meaning all parameters passed to the batch file, which can then be used like so: batchfile.bat path_to_folder

• The dir command is used to determine if the folder is hidden, then if it is marked system. In each case, It searches for the folder with filtering so only those with the appropriate attribute set are found. If it can find the folder with that attribute filter, errorlevel is set to 0. Otherwise it is 1. >nul 2>nul prevents stdout and stderr output.

• If it can find that either the hidden or system attribute is set, it jumps (goto) the :clear label, where those attributes are unset.

• If it can't find those attributes set, it sets both of them.

-
Great explanation, but somehow it didn't work :/ –  Daniel Apr 9 '12 at 1:32
But this worked, 'Set _Path=C:\foldername 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%"*.* )' –  Daniel Apr 9 '12 at 1:33
@echo off
Set /P Answer=Do you wish to hide File (Y/N)?
:yes
attrib +h Foldername
pause
exit
:no
attrib -h Foldername
pause
exit


This batch file will make it that if you choose Y for yes, it will hide the file and if you choose N for no, it will unhide the file. Original batchfile.

-
cls
/@echo off
/title Folder Locker
/IF EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B303­­09D}" GOTO UNLOCK
/IF NOT EXIST Locker GOTO MDLOCKER
/echo Folder Created.
/:CONFIRM
/echo Are you sure you 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-08002B303­­09D}"
/attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B303­­09D}"
/echo Folder Locked.
/GOTO END

/:UNLOCK
/echo Enter password to unlock the Folder :
/set/p "pass="
/IF NOT %pass% == YourPassword GOTO FAIL
/attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B303­­09D}"
/ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B303­­09D}" LOCK
/echo Folder Unlocked Successfully.
/GOTO END
/:FAIL
/GOTO END

/:MDLOCKER
/md Locker
/echo Folder created.

/GOTO END
/:END
/PAUSE


!!!!!IMPORTANT!!!!
WHERE IT SAYS IF NOT %pass% == YourPassword GOTO FAIL CHANGE YOURPASSWORD TO WHAT YOU WANT YOUR PASS WORD TO BE. Where I put the "/" indicates pressing enter

-

I have literally just used this to hide folders on a memory stick unless the user enters a correct password. Here's how I did it:

attrib +h E:\MyFolderIWantToHide /d /s
if %PASS%==password1 attrib -h E:\MyFolderIWantToHide /d /s


On opening the file the "MyFolderIWantToHide" folder is instantly hidden. This is the

attrib +h E:\MyFolderIWantToHide /d /s


part, where +h applies the hidden setting. You need the /d and /s switches to affect folders and not files, and E:\MyFolderIWantToHide is just the path to the folder I want to hide. You can find out more about attrib in cmd, type 'attrib /?'.

Then it asks for a password using set /p [VAR NAME]= and, if the variable is equal to whatever your password is (mine being password1) it will remove the attribute using -h instead of +h.

-

This will work with files or folders you can either run this from a command line using %1 to name the file or folder you wish to show/hide or set the name internally you can also drop a visible file or folder onto the batch file:

@Echo Off
Set Input=%1
If %Input%' NEQ ' GoTo Main
Set Input="Place File Name or Folder Name Here"
GoTo Main

:Main
If Exist %Input%\nul (
) ELSE (
Dir /B/A-h %Input%
)
If %ErrorLevel% EQU 0 (
GoTo Hide
) ELSE (
GoTo Show
)

:Show
Attrib -H %Input%
GoTo :EOF

:Hide
Attrib +H %Input%
GoTo :EOF

-