Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
@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
add comment

7 Answers

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.

share|improve this answer
    
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
add comment

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

share|improve this answer
    
Thanks, works perfectly! –  Daniel Apr 9 '12 at 1:31
add comment

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.

share|improve this answer
    
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
add comment
@echo off
Set /P Answer=Do you wish to hide File (Y/N)?
if /I {%ANSWER%}=={y} (goto :yes)
if /I {%ANSWER%}=={Y} (goto :yes)
if /I {%ANSWER%}=={n} (goto :no)
if /I {%ANSWER%}=={N} (goto :no)
: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.

share|improve this answer
add comment
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
/echo Invalid Password!
/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

share|improve this answer
add comment

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
set /p PASS=Password: 
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.

share|improve this answer
add comment

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 (
Dir /B/Ad-h %Input%
) 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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.