Windows批处理文件有哪些不太为人所知,但很重要和有用的特性?

指南:

每个答案一个特征 给出特性的简短描述和示例,而不仅仅是文档链接 将答案限制在本地功能,即不需要额外的软件,如Windows资源包

澄清:这里我们指的是由cmd.exe处理的脚本,这是WinNT变体的默认值。

(请参见:Windows批处理文件:.bat vs .cmd?)


当前回答

PUSHD path

将您带到path指定的目录。

POPD

带您回到您“推”的目录。

其他回答

整数运算:

> SET /A result=10/3 + 1
4

这里有一个技巧,我用它来连续运行My Nant Build脚本,而不必一遍又一遍地单击批处理文件。

:CODELINE
NANT.EXE -buildfile:alltargets.build -l:build.log build.product
@pause
GOTO :CODELINE

在您的解决方案完成构建之后,它将被暂停。然后,如果您按下任何键,它将再次重新运行构建脚本。我必须说非常方便。

没有提供太多的功能,但是您可以将title命令用于一些用途,比如在任务栏中提供长脚本的状态,或者只是增强用户反馈。

@title Searching for ...
:: processing search
@title preparing search results
:: data processing

搜索路径上的可执行文件(或其他类似路径的字符串,如果需要):

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:\>

dos命令宏。

我已经很久没有参考过这一点了,但我仍然认为这是一个好主意,值得分享。

我们可以将批处理文件和doskey脚本合并到一个文件中。这可能看起来有点过于聪明,但它确实有效。

;= @echo off
;= rem Call DOSKEY and use this file as the macrofile
;= %SystemRoot%\system32\doskey /listsize=1000 /macrofile=%0%
;= rem In batch mode, jump to the end of the file
;= goto end

;= Doskey aliases
h=doskey /history

;= File listing enhancements
ls=dir /x $*

;= Directory navigation
up=cd ..
pd=pushd

;= :end
;= rem ******************************************************************
;= rem * EOF - Don't remove the following line.  It clears out the ';' 
;= rem * macro. Were using it because there is no support for comments
;= rem * in a DOSKEY macro file.
;= rem ******************************************************************
;=

它通过定义一个假的doskey宏';'来工作,当它被解释为批处理文件时,它会被优雅地(或无声地)忽略。

我缩短了这里列出的版本,如果你想要更多,请点击这里。