Windows批处理文件有哪些不太为人所知,但很重要和有用的特性?
指南:
每个答案一个特征 给出特性的简短描述和示例,而不仅仅是文档链接 将答案限制在本地功能,即不需要额外的软件,如Windows资源包
澄清:这里我们指的是由cmd.exe处理的脚本,这是WinNT变体的默认值。
(请参见:Windows批处理文件:.bat vs .cmd?)
Windows批处理文件有哪些不太为人所知,但很重要和有用的特性?
指南:
每个答案一个特征 给出特性的简短描述和示例,而不仅仅是文档链接 将答案限制在本地功能,即不需要额外的软件,如Windows资源包
澄清:这里我们指的是由cmd.exe处理的脚本,这是WinNT变体的默认值。
(请参见:Windows批处理文件:.bat vs .cmd?)
当前回答
PUSHD path
将您带到path指定的目录。
POPD
带您回到您“推”的目录。
其他回答
一行中包含多个命令,在很多情况下都很有用:
&用于组合两个命令,执行command1和command2 &&一个条件组合,如果command1成功完成,则执行command2 仅当command1未成功完成时才执行Command2。
例子:
:: ** Edit the most recent .TXT file and exit, useful in a .CMD / .BAT **
FOR /F %%I IN ('DIR *.TXT /B /O:-N') DO NOTEPAD %%I & EXIT
:: ** If exist any .TXT file, display the list in NOTEPAD, if not it
:: ** exits without any error (note the && and the 2> error redirection)
DIR *.TXT > TXT.LST 2> NUL && NOTEPAD TXT.LST
搜索路径上的可执行文件(或其他类似路径的字符串,如果需要):
c:\> for %i in (cmd.exe) do @echo. %~$PATH:i
C:\WINDOWS\system32\cmd.exe
c:\> for %i in (python.exe) do @echo. %~$PATH:i
C:\Python25\python.exe
c:\>
隐藏交互式批处理脚本的输入:
@echo off
echo hP1X500P[PZBBBfh#b##fXf-V@`$fPf]f3/f1/5++u5>in.com
set /p secret_password="Enter password:"<nul
for /f "tokens=*" %%i in ('in.com') do (set secret_password=%%i)
del in.com
使用空格和转义字符完全控制输出:
echo. ^<resourceDir^>/%basedir%/resources^</resourceDir^>
IF命令!没有它,我的批处理文件是垃圾!
@echo off
IF exist %windir%\system32\iexplore.exe goto end
echo Hmm... it seems you do not have Internet Explorer.
echo Great! You seem to understand ;)
:end
echo Hmm... You have Internet Explorer.
echo That is bad :)