0

I want to toggle a directory with the attribute -h & +h accordingly.

When I use this command sequence the output is

\directory was unexpected at the time.

Any ideas on how to get this working?

:toggle
if attrib \directory /s /d equ -h goto hidedir
if attrib \directory /s /d equ +h goto showdir
pause
goto start

:showdir
attrib -r -s -h \directory /s /d
goto start

:hidedir
attrib +r +s +h \directory /s /d
goto start
Mofi
  • 46,139
  • 17
  • 80
  • 143
BenZyMo
  • 1
  • 1

4 Answers4

0

Here is a little batch code to toggle hidden attribute of a specified directory using the technique as explained on question How to get attributes of a file using batch file?

@echo off
set "Directory=C:\Temp\Test Directory"
for %%D in ("%Directory%") do set "Attributes=%%~aD"
if "%Attributes:~3,1%"=="h" goto UnhideDir

%SystemRoot%\System32\attrib.exe +h "%Directory%"
goto :EOF

:UnhideDir
%SystemRoot%\System32\attrib.exe -h "%Directory%"

The information required to understand FOR loop and %%~aD is output by entering in a command prompt window for /? or help for.

And running set /? or help set in a command prompt window and reading the help printed helps to understand the substring comparison in IF condition which compares just fourth character in string with the attributes of the directory. (First character is referenced with index value 0.)

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
0
@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Configure folder to process
    set "folder=.\test"

    rem Determine the needed attribute
    set "newMode=+h"
    for %%f in ("%folder%") do for /f "tokens=2 delims=h" %%a in ("%%~af") do set "newMode=-h"

    rem Change the attribute to new mode for the indicated folder and its contents
    "%systemRoot%\system32\attrib.exe" %newMode% "%folder%" 
    "%systemRoot%\system32\attrib.exe" %newMode% "%folder%\*" /s /d

In this code a for is used to obtain a reference to the folder and a for /f to determine the presence of the h attrib in the list of folder attributes. The variable %newMode% will get the required attrib command parameter to adjust the attributes of the folder an its contents

MC ND
  • 69,615
  • 8
  • 84
  • 126
0
@echo off
setlocal
attrib \directory /d | findstr "^....H" && set "switch=-" || set "switch=+"
attrib %switch%r %switch%s %switch%h \directory /s /d
dbenham
  • 127,446
  • 28
  • 251
  • 390
-1

I think this might help just change the links to what ever command you want

@echo off
for /f "delims=" %%a in (work.txt) do (
  set NUM=%%a
)

if %NUM%==1 goto start
echo incorrect
goto close

:start 
start "Youtube playlist" "https://www.youtube.com"
echo 2 > work.txt 
exit

:close 
start "imager" "https://imgur.com/"
echo 1 > work.txt
exit
Stephan
  • 53,940
  • 10
  • 58
  • 91