【问题标题】:Make a toggle "switch" in batch file在批处理文件中进行切换“开关”
【发布时间】:2014-11-28 16:45:48
【问题描述】:
我想相应地切换具有-h 和+h 属性的目录。
当我使用这个命令序列时,输出是
\directory was unexpected at the time.
关于如何让它发挥作用的任何想法?
: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
【问题讨论】:
标签:
batch-file
toggle
hide
show
【解决方案1】:
我认为这可能有助于将链接更改为您想要的任何命令
@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
【解决方案2】:
@echo off
setlocal
attrib \directory /d | findstr "^....H" && set "switch=-" || set "switch=+"
attrib %switch%r %switch%s %switch%h \directory /s /d
【解决方案3】:
@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
在此代码中,for 用于获取对文件夹的引用,for /f 用于确定文件夹属性列表中是否存在h 属性。变量%newMode% 将获取所需的attrib 命令参数来调整文件夹的属性及其内容
【解决方案4】:
这是一个小批处理代码,用于使用问题 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%"
通过在命令提示符窗口中输入for /? 或help for 来输出理解FOR 循环和%%~aD 所需的信息。
在命令提示符窗口中运行set /? 或help set 并阅读打印的帮助有助于理解IF 条件中的子字符串比较,该条件仅将字符串中的第四个字符与目录的属性进行比较。 (第一个字符用索引值 0 引用。)