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

指南:

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

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

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


当前回答

PAUSE

停止执行并显示如下提示:

Press any key to continue . . .

如果你想通过在Windows资源管理器中双击一个批处理来运行它,并且想实际看到输出,而不仅仅是命令窗口的一闪而过,这很有用。

其他回答

不知道这在批处理文件中有多有用,但在命令提示符中使用它是一个非常方便的命令:

C:\some_directory> start .

这将在“some_directory”文件夹中打开Windows资源管理器。

我发现这非常节省时间。

There is also the EDLIN command. While it may be an old bastard tool once used for line-based text editing, the fact that it's controllable from the command line makes it rather useful for batch scripting, mostly because, just like any other case you'd be using EDLIN, it's the only tool available. After all, EDLIN is not a tool you would ordinarily want to use for text editing, unless you are somewhat masochistic. To quote Tim Patterson (the fellow who wrote it): "I was aghast when I heard that IBM was using it and not throwing it out the window."

注意:EDLIN将老式的EOF (1A)标记添加到它编辑的文件中。如果需要删除它们,可能必须使用DEBUG。

Forfiles非常有用,例如,递归删除所有超过两天的文件

forfiles /D -2 /P "C:\Temp" /S /C "cmd /c del @path"

调用集——将环境变量展开几个层次。

在http://ss64.com/nt/call.html#advanced从另一个SO问题的答案中发现这个批处理文件变量在for循环中初始化

set VarName=Param
set Param=This

call set Answer=%%%Varname%%%
Echo %Answer%

给了

set VarName=Param
set Param=This
call set Answer=%Param%
Echo This
This

设置环境变量时的搜索和替换:

> @set fname=%date:/=%

...从日期中删除“/”以用于带时间戳的文件名。

还有子字符串……

> @set dayofweek=%fname:~0,3%