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

指南:

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

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

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


当前回答

PAUSE

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

Press any key to continue . . .

如果你想通过在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。

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

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

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

关于使用::而不是REM进行评论:要小心!::是CALL标签的一种特殊情况,作用类似于注释。当在括号内使用时,例如在for或IF循环中,函数将提前退出。调试非常令人沮丧!

详细描述请参见http://www.ss64.com/nt/rem.html。

(添加一个新的答案,而不是上面第一次提到的评论,因为我还不值得评论:0)

要从文件的第一行开始设置一个环境变量,我使用以下命令:

rem a.txt contains one line: abc123
set /p DATA=<a.txt
echo data: %DATA%

这将输出:abc123