Windows 批处理文件:.bat 还是 .cmd?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/148968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:26:07  来源:igfitidea点击:

Windows batch files: .bat vs .cmd?

windowsbatch-filecmd

提问by Chris Noe

As I understand it, .batis the old 16-bit naming convention, and .cmdis for 32-bit Windows, i.e., starting with NT. But I continue to see .bat files everywhere, and they seem to work exactly the same using either suffix. Assuming that my code will never need to run on anything older than NT, does it really matter which way I name my batch files, or is there some gotchaawaiting me by using the wrong suffix?

据我了解,.bat是旧的 16 位命名约定,.cmd适用于 32 位 Windows,即以 NT 开头。但是我继续在任何地方看到 .bat 文件,并且它们似乎使用任一后缀都完全相同。假设我的代码永远不需要在比 NT 旧的任何东西上运行,我命名批处理文件的方式真的很重要,或者是否有一些问题等待我使用错误的后缀?

采纳答案by Ben Hoffstein

From this news group postingby Mark Zbikowskihimself:

来自Mark Zbikowski本人发布这个新闻组

The differences between .CMD and .BAT as far as CMD.EXE is concerned are: With extensions enabled, PATH/APPEND/PROMPT/SET/ASSOC in .CMD files will set ERRORLEVEL regardless of error. .BAT sets ERRORLEVEL only on errors.

就 CMD.EXE 而言,.CMD 和 .BAT 之间的区别在于: 启用扩展后,.CMD 文件中的 PATH/APPEND/PROMPT/SET/ASSOC 将设置 ERRORLEVEL,而不管错误如何。.BAT 仅在错误时设置 ERRORLEVEL。

In other words, if ERRORLEVEL is set to non-0 and then you run one of those commands, the resulting ERRORLEVEL will be:

换句话说,如果 ERRORLEVEL 设置为非 0,然后您运行这些命令之一,则结果 ERRORLEVEL 将是:

  • left alone at its non-0 value in a .bat file
  • reset to 0 in a .cmd file.
  • 在 .bat 文件中保留其非 0 值
  • 在 .cmd 文件中重置为 0。

回答by Chris Noe

Here is a compilation of verified information from the various answers and cited references in this thread:

以下是该主题中各种答案和引用参考资料中经过验证的信息的汇编:

  1. command.comis the 16-bit command processor introduced in MS-DOS and was also used in the Win9x series of operating systems.
  2. cmd.exeis the 32-bit command processor in Windows NT (64-bit Windows OSes also have a 64-bit version). cmd.exewas never part of Windows 9x. It originated in OS/2 version 1.0, and the OS/2 version of cmdbegan 16-bit (but was nonetheless a fully fledged protected mode program with commands like start). Windows NT inherited cmdfrom OS/2, but Windows NT's Win32 version started off 32-bit. Although OS/2 went 32-bit in 1992, its cmdremained a 16-bit OS/2 1.x program.
  3. The ComSpecenv variable defines which program is launched by .batand .cmdscripts. (Starting with WinNT this defaults to cmd.exe.)
  4. cmd.exeis backward compatible with command.com.
  5. A script that is designed for cmd.execan be named .cmdto prevent accidental execution on Windows 9x. This filename extension also dates back to OS/2 version 1.0 and 1987.
  1. command.com是 MS-DOS 中引入的 16 位命令处理器,也用于 Win9x 系列操作系统。
  2. cmd.exe是 Windows NT 中的 32 位命令处理器(64 位 Windows 操作系统也有 64 位版本)。cmd.exe从来都不是 Windows 9x 的一部分。它起源于 OS/2 1.0 版,并且 OS/2 版本cmd开始于 16 位(但仍然是一个完全成熟的保护模式程序,带有诸如 之类的命令start)。Windows NT 继承cmd自 OS/2,但 Windows NT 的 Win32 版本是从 32 位开始的。尽管 OS/2 在 1992 年变成了 32 位,但它cmd仍然是一个 16 位的 OS/2 1.x 程序。
  3. ComSpec环境变量定义哪些程序由发射.bat.cmd脚本。(从 WinNT 开始,默认为cmd.exe。)
  4. cmd.exe向后兼容command.com.
  5. 设计用于的脚本cmd.exe可以命名.cmd以防止在 Windows 9x 上意外执行。这个文件扩展名也可以追溯到 OS/2 版本 1.0 和 1987。

Here is a list of cmd.exefeatures that are not supported by command.com:

以下是cmd.exe不支持的功能列表command.com

  • Long filenames (exceeding the 8.3 format)
  • Command history
  • Tab completion
  • Escape character: ^(Use for: \ & | > < ^)
  • Directory stack: PUSHD/POPD
  • Integer arithmetic: SET /A i+=1
  • Search/Replace/Substring: SET %varname:expression%
  • Command substitution: FOR /F(existed before, has been enhanced)
  • Functions: CALL :label
  • 长文件名(超过 8.3 格式)
  • 命令历史
  • 标签完成
  • 转义字符:^(适用于:\ & | > < ^
  • 目录栈:PUSHD/POPD
  • 整数运算: SET /A i+=1
  • 搜索/替换/子字符串: SET %varname:expression%
  • 命令替换:(FOR /F之前存在,已增强)
  • 职能: CALL :label

Order of Execution:

执行顺序:

If both .bat and .cmd versions of a script (test.bat, test.cmd) are in the same folder and you run the script without the extension (test), by default the .bat version of the script will run, even on 64-bit Windows 7. The order of execution is controlled by the PATHEXT environment variable. See Order in which Command Prompt executes filesfor more details.

如果脚本 (test.bat, test.cmd) 的 .bat 和 .cmd 版本都在同一个文件夹中,并且您运行没有扩展名 (test) 的脚本,则默认情况下,脚本的 .bat 版本将运行,即使在 64 位 Windows 7 上。执行顺序由 PATHEXT 环境变量控制。有关更多详细信息,请参阅命令提示符执行文件的顺序

References:

参考:

wikipedia: Comparison of command shells

维基百科:命令外壳的比较

回答by Gringo Suave

These answers are a bit too long and focused on interactive use. The important differences for scripting are:

这些答案有点太长,并且侧重于交互式使用。脚本的重要区别是:

  • .cmdprevents inadvertent execution on non-NT systems.
  • .cmdenables built-in commands to change Errorlevel to 0 on success.
  • .cmd防止在非 NT 系统上无意执行。
  • .cmd启用内置命令以在成功时将 Errorlevel 更改为 0。

Not that exciting, eh?

没那么刺激吧?

There used to be a number of additional features enabled in .cmdfiles, called Command Extensions. However, they are now enabled by default for both .batand .cmdfiles under Windows 2000 and later.

过去在.cmd文件中启用了许多附加功能,称为命令扩展。但是,在 Windows 2000 及更高版本下,它们现在默认为.bat.cmd文件启用。

Bottom line:in 2012 and beyond, I recommend using .cmdexclusively.

底线:在 2012 年及以后,我建议只使用.cmd

回答by Michael Burr

No - it doesn't matter in the slightest. On NT the .bat and .cmd extension both cause the cmd.exe processor to process the file in exactly the same way.

不 - 一点都不重要。在 NT 上,.bat 和 .cmd 扩展名都使 cmd.exe 处理器以完全相同的方式处理文件。

Additional interesting information about command.com vs. cmd.exe on WinNT-class systems from MS TechNet (http://technet.microsoft.com/en-us/library/cc723564.aspx):

来自 MS TechNet ( http://technet.microsoft.com/en-us/library/cc723564.aspx) 的WinNT 类系统上有关 command.com 与 cmd.exe 的其他有趣信息:

This behavior reveals a quite subtle feature of Windows NT that is very important. The 16-bit MS-DOS shell (COMMAND.COM) that ships with Windows NT is specially designed for Windows NT. When a command is entered for execution by this shell, it does not actually execute it. Instead, it packages the command text and sends it to a 32-bit CMD.EXE command shell for execution. Because all commands are actually executed by CMD.EXE (the Windows NT command shell), the 16-bit shell inherits all the features and facilities of the full Windows NT shell.

这种行为揭示了 Windows NT 非常重要的一个非常微妙的特性。Windows NT 附带的 16 位 MS-DOS 外壳程序 (COMMAND.COM) 是专为 Windows NT 设计的。当此 shell 输入要执行的命令时,它实际上并没有执行它。相反,它将命令文本打包并将其发送到 32 位 CMD.EXE 命令外壳以供执行。因为所有命令实际上都是由 CMD.EXE(Windows NT 命令外壳程序)执行的,所以 16 位外壳程序继承了完整的 Windows NT 外壳程序的所有功能和功能。

回答by David Gray

RE:Apparently when command.com is invoked is a bit of a complex mystery;

RE:显然,command.com 何时被调用是一个复杂的谜;

Several months ago, during the course of a project, we had to figure out why some programs that we wanted to run under CMD.EXE were, in fact, running under COMMAND.COM. The "program" in question was a very old .BAT file, that still runs daily.

几个月前,在一个项目的过程中,我们不得不弄清楚为什么我们想要在 CMD.EXE 下运行的某些程序实际上是在 COMMAND.COM 下运行的。有问题的“程序”是一个非常古老的 .BAT 文件,它仍然每天运行。

We discovered that the reason the batch file ran under COMMAND.COM is that it was being started from a .PIF file (also ancient). Since the special memory configuration settings available only through a PIF have become irrelevant, we replaced it with a conventional desktop shortcut.

我们发现批处理文件在 COMMAND.COM 下运行的原因是它是从 .PIF 文件(也是古老的)启动的。由于仅通过 PIF 可用的特殊内存配置设置已变得无关紧要,我们将其替换为传统的桌面快捷方式。

The same batch file, launched from the shortcut, runs in CMD.EXE. When you think about it, this makes sense. The reason that it took us so long to figure it out was partially due to the fact that we had forgotten that its item in the startup group was a PIF, because it had been in production since 1998.

从快捷方式启动的同一个批处理文件在 CMD.EXE 中运行。当你想到它时,这是有道理的。我们花了这么长时间才弄明白,部分原因是我们忘记了它在启动组中的项目是 PIF,因为它自 1998 年以来一直在生产。

回答by tvCa

Still, on Windows 7, BAT files have also this difference : If you ever create files TEST.BAT and TEST.CMD in the same directory, and you run TEST in that directory, it'll run the BAT file.

尽管如此,在 Windows 7 上,BAT 文件也有这个区别:如果您在同一目录中创建文件 TEST.BAT 和 TEST.CMD,并且您在该目录中运行 TEST,它将运行 BAT 文件。

C:\>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

C:\Temp>echo echo bat > test.bat

C:\Temp>echo echo cmd > test.cmd

C:\Temp>test

C:\Temp>echo bat
bat

C:\Temp>

回答by Rob at TVSeries.com

Since the original post was regarding the consequences of using the .bat or .cmd suffix, not necessarily the commands insidethe file...

由于原来的文章是关于使用.bat或.cmd的后果后缀,不一定是命令里面的文件...

One other difference between .bat and .cmd is that if two files exist with the same file name and both those extensions, then:

.bat 和 .cmd 之间的另一个区别是,如果存在两个文件名和扩展名相同的文件,则:

  • entering filenameor filename.bat at the command line will run the .bat file

  • to run the .cmd file, you have to enter filename.cmd

  • 在命令行输入filenamefilename.bat 将运行 .bat 文件

  • 要运行 .cmd 文件,您必须输入文件名.cmd

回答by Lorenzo Boccaccia

everything working in a batch should work in a cmd; cmd provides some extensions for controlling the environment. also, cmd is executed by in new cmd interpreter and thus should be faster (not noticeable on short files) and stabler as bat runs under the NTVDM emulated 16bit environment

批处理中的所有内容都应在 cmd 中运行;cmd 提供了一些用于控制环境的扩展。此外,cmd 由新的 cmd 解释器执行,因此应该更快(在短文件上不明显)和更稳定,因为 bat 在 NTVDM 模拟的 16 位环境下运行

回答by Patrick Cuff

I believe if you change the value of the ComSpec environment variable to %SystemRoot%system32\cmd.exe(CMD) then it doesn't matter if the file extension is .BATor .CMD. I'm not sure, but this may even be the default for WinXP and above.

我相信,如果你改变COMSPEC环境变量的值%SystemRoot%system32\cmd.exe(CMD),那么它并不重要,如果文件扩展名是.BAT.CMD。我不确定,但这甚至可能是 WinXP 及更高版本的默认设置。

回答by zask

.cmd and .bat file execution is different because in a .cmd errorlevel variable it can change on a command that is affected by command extensions. That's about it really.

.cmd 和 .bat 文件执行是不同的,因为在 .cmd errorlevel 变量中,它可以在受命令扩展影响的命令上更改。真的是这样。