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

指南:

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

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

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


当前回答

您可以将if语句链接起来,以获得类似短路布尔' and'的效果。

if foo if bar baz

其他回答

我真的很喜欢这个Windows XP命令参考,以及在顶部的语法链接;它涵盖了许多已经在其他答案中找到的技巧和技巧。

'remove directory'上的子目录选项:

rd /s /q junk

将输出重定向到控制台,即使批处理的输出已经通过> con语法重定向到文件。

例子: foo.cmd:

echo a
echo b > con

调用:

foo.cmd > output.txt

这将导致“a”输出到output.txt,而“b”输出到控制台。

我发现你可以轻松地将命令输出重定向到文件中非常有用:

DIR *.txt > tmp.txt
DIR *.exe >> tmp.txt

单箭头创建或覆盖文件,双箭头追加文件。现在我可以在我的文本编辑器中打开tmp.txt,做各种各样的事情。

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。