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

指南:

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

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

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


当前回答

符号链接:

mklink /d directorylink ..\realdirectory
mklink filelink realfile

该命令是Windows Server 2008及更新版本(包括Vista和Windows 7)上的本机命令。(它也包含在一些Windows资源包中。)

其他回答

如块状结构:

if "%VS90COMNTOOLS%"=="" (
  echo: Visual Studio 2008 is not installed
  exit /b
)

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。

一个非常古老(大约1990年)的技巧来获得环境变量的总大小:

set > test
dir test
del test

相当于bash(和其他shell)

echo -n Hello # or
echo Hello\\c

输出“Hello”,后面没有换行符。一个cmd黑客来做这个:

<nul set /p any-variable-name=Hello

Set /p是一种提示用户输入的方法。它发出给定的字符串,然后等待(在同一行,即没有CRLF),等待用户输入响应。

<nul只是向set /p命令输送一个空响应,因此最终结果是发出的提示字符串。(由于响应为空,所使用的变量保持不变。)

问题是:不可能输出前导等号,在Vista中前导空白字符被删除,但在XP中没有。

允许您更改目录基于环境变量没有 必须指定'%'指令。如果指定的变量没有 存在,然后尝试目录名。

@if defined %1 (call cd "%%%1%%") else (call cd %1)