Windows批处理文件:.bat vs .cmd?

时间:2021-12-28 02:05:49

As I understand it, .bat is the old 16-bit naming convention, and .cmd is 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 gotcha awaiting me by using the wrong suffix?

正如我所理解的,.bat是旧的16位命名约定,而.cmd是32位窗口,即。从NT开始,但是我继续看到。bat文件到处都是,而且它们使用后缀都是完全一样的。假设我的代码永远不需要在任何大于NT的东西上运行,那么我是用哪种方式命名我的批处理文件呢,还是用错误的后缀来等待我呢?

15 个解决方案

#1


366  

From this news group posting by Mark Zbikowski himself:

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和。bat之间的差异。EXE关注的是:在启用扩展的情况下,在. cmd文件中,PATH/APPEND/PROMPT/SET/ASSOC将设置ERRORLEVEL,而不考虑错误,只在错误上设置错误级别。

#2


360  

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

以下是来自不同答案的验证信息的汇编,并引用了该线程中的引用:

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

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

这是cmd的列表。exe特性不受command.com的支持:

  • Long filenames (exceeding the 8.3 format)
  • 长文件名(超过8.3格式)
  • Command history
  • 命令历史记录
  • Tab completion
  • 选项卡完成
  • Escape character: ^ (Use for: \ & | > < ^)
  • 转义字符:(用于:\ & | > <)
  • Directory stack: PUSHD/POPD
  • 目录堆栈:PUSHD和POPD
  • Integer arithmetic: SET /A i+=1
  • 整数算术:SET /A i+=1。
  • Search/Replace/Substring: SET %varname:expression%
  • 搜索/替换/子字符串:% % varname:表达式
  • Command substitution: FOR /F (existed before, has been enhanced)
  • 命令替换:FOR /F(以前存在,已增强)
  • Functions: 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 files for more details.

如果是。bat和。cmd版本的脚本(测试。在同一个文件夹中,如果没有扩展(测试),就运行脚本,默认情况下,脚本的.bat版本将运行,即使是64位Windows 7。执行顺序由PATHEXT环境变量控制。参见命令提示符执行文件以获取更多细节。

References:

引用:

wikipedia: Comparison of command shells

wikipedia:命令shell的比较。

#3


39  

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

这些答案有点太长,专注于交互使用。重要的区别是:

  • .cmd prevents inadvertent execution on non-NT systems.
  • .cmd防止无意执行非nt系统。
  • .cmd enables built-in commands to change Errorlevel to 0 on success.
  • .cmd允许内置命令在成功时将Errorlevel更改为0。

Edit: Command Extensions are on by default in both .bat and .cmd files under Windows 2000 or later.

编辑:命令扩展在Windows 2000或以后的。bat和。cmd文件中都是默认的。

In 2012 and beyond, I recommend using .cmd exclusively.

在2012年及以后,我建议只使用。cmd。

#4


25  

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

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

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的一个非常重要的特性。16位MS-DOS shell (COMMAND.COM)是专门为Windows NT设计的,当一个命令被这个shell执行时,它实际上并没有执行它。相反,它将命令文本打包并发送到一个32位的CMD。EXE命令shell执行。因为所有的命令都是由CMD执行的。EXE (Windows NT命令shell), 16位shell继承了所有Windows NT外壳的所有特性和功能。

#5


15  

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.

我们发现批处理文件运行在命令下的原因。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年开始生产的。

#6


13  

Since the original post was regarding the consequences of using the .bat or .cmd suffix, not necessarily the commands inside the 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 filename or filename.bat at the command line will run the .bat file

    输入文件名或文件名。命令行中的bat将运行.bat文件。

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

    要运行.cmd文件,您必须输入filename.cmd。

#7


10  

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文件也有这种区别:如果您曾经创建过文件测试。蝙蝠和测试。在同一个目录下的CMD,在那个目录中运行测试,它将运行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>

#8


8  

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解释器执行的,因此在NTVDM仿真的16位环境下,应该更快(在短文件中不明显)和stabler。

#9


3  

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

我相信如果您将ComSpec环境变量的值更改为%SystemRoot%system32\cmd。如果文件扩展名是. bat或. cmd,那就无关紧要了。我不确定,但这可能是WinXP和上面的默认设置。

#10


3  

Slightly off topic, but have you considered Windows Scripting Host? You might find it nicer.

稍微偏离主题,但是您考虑过Windows脚本主机吗?你可能会觉得它更好。

#11


2  

The extension makes no difference. There are slight differences between COMMAND.COM handling the file vs. CMD.EXE

扩展没有区别。命令之间有细微的差别。处理文件与CMD.EXE。

#12


2  

.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变量中,它可以更改受命令扩展影响的命令。这是真的。

#13


-2  

Here is one difference I discovered: EnableDelayedExpansion is required in .cmd files.
Where as in case of .bat files it is implicit by default. (Windows 10)

这里有一个我发现的区别:EnableDelayedExpansion需要在.cmd文件中。如果是。bat文件,默认情况下是隐式的。(Windows 10)

dir *? | find /i "FOOBAR"
if ERRORLEVEL 0             (
set result="found"  ) else  (
set result="not found"  )
echo %result%

This works in .bat but is always found in case of a .cmd file.
Changing line 2 to the following makes it work as expected:

这在.bat中有效,但总是在.cmd文件中找到。将第2行改为如下所示:

if %ERRORLEVEL% equ 0       (

And finally for the .cmd file this works correctly:

最后,对于。cmd文件,这是正确的:

setLocal EnableDelayedExpansion
...
if !ErrorLevel! equ 1       (
...

#14


-2  

As being a Cmd programmer, and looking all over the web, it really doesn't matter which one you use, you can have a .bat program on a Windows 7, and run it on a Windows 10. but if you are to make it on a Windows 10, you probably won't be able to run all the commands on a Windows 7. A .cmd is the exact same way, and runs the exact same program and codes.

作为一名Cmd程序员,在网络上到处寻找,不管你使用哪一个,你都可以在Windows 7上拥有一个.bat程序,并在Windows 10上运行它。但是如果你要在Windows 10上运行,你很可能无法在Windows 7上运行所有的命令。一个。cmd是完全相同的方式,并且运行完全相同的程序和代码。

All the difference is, is that its a different name of the same program, as long as it is connected to CMD.EXE, it runs the same commands.

不同之处在于,它是同一个程序的不同名称,只要它与CMD连接。EXE,它运行相同的命令。

#15


-10  

a difference:

一个区别:

.cmd files are loaded into memory before being executed. .bat files execute a line, read the next line, execute that line...

.cmd文件在执行之前被加载到内存中。.bat文件执行一行,读取下一行,执行该行…

you can come across this when you execute a script file and then edit it before it's done executing. bat files will be messed up by this, but cmd files won't.

当您执行一个脚本文件并在它执行之前编辑它时,您会遇到这个问题。bat文件将会被这个问题搞砸,但是cmd文件不会。

#1


366  

From this news group posting by Mark Zbikowski himself:

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和。bat之间的差异。EXE关注的是:在启用扩展的情况下,在. cmd文件中,PATH/APPEND/PROMPT/SET/ASSOC将设置ERRORLEVEL,而不考虑错误,只在错误上设置错误级别。

#2


360  

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

以下是来自不同答案的验证信息的汇编,并引用了该线程中的引用:

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

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

这是cmd的列表。exe特性不受command.com的支持:

  • Long filenames (exceeding the 8.3 format)
  • 长文件名(超过8.3格式)
  • Command history
  • 命令历史记录
  • Tab completion
  • 选项卡完成
  • Escape character: ^ (Use for: \ & | > < ^)
  • 转义字符:(用于:\ & | > <)
  • Directory stack: PUSHD/POPD
  • 目录堆栈:PUSHD和POPD
  • Integer arithmetic: SET /A i+=1
  • 整数算术:SET /A i+=1。
  • Search/Replace/Substring: SET %varname:expression%
  • 搜索/替换/子字符串:% % varname:表达式
  • Command substitution: FOR /F (existed before, has been enhanced)
  • 命令替换:FOR /F(以前存在,已增强)
  • Functions: 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 files for more details.

如果是。bat和。cmd版本的脚本(测试。在同一个文件夹中,如果没有扩展(测试),就运行脚本,默认情况下,脚本的.bat版本将运行,即使是64位Windows 7。执行顺序由PATHEXT环境变量控制。参见命令提示符执行文件以获取更多细节。

References:

引用:

wikipedia: Comparison of command shells

wikipedia:命令shell的比较。

#3


39  

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

这些答案有点太长,专注于交互使用。重要的区别是:

  • .cmd prevents inadvertent execution on non-NT systems.
  • .cmd防止无意执行非nt系统。
  • .cmd enables built-in commands to change Errorlevel to 0 on success.
  • .cmd允许内置命令在成功时将Errorlevel更改为0。

Edit: Command Extensions are on by default in both .bat and .cmd files under Windows 2000 or later.

编辑:命令扩展在Windows 2000或以后的。bat和。cmd文件中都是默认的。

In 2012 and beyond, I recommend using .cmd exclusively.

在2012年及以后,我建议只使用。cmd。

#4


25  

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

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

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的一个非常重要的特性。16位MS-DOS shell (COMMAND.COM)是专门为Windows NT设计的,当一个命令被这个shell执行时,它实际上并没有执行它。相反,它将命令文本打包并发送到一个32位的CMD。EXE命令shell执行。因为所有的命令都是由CMD执行的。EXE (Windows NT命令shell), 16位shell继承了所有Windows NT外壳的所有特性和功能。

#5


15  

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.

我们发现批处理文件运行在命令下的原因。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年开始生产的。

#6


13  

Since the original post was regarding the consequences of using the .bat or .cmd suffix, not necessarily the commands inside the 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 filename or filename.bat at the command line will run the .bat file

    输入文件名或文件名。命令行中的bat将运行.bat文件。

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

    要运行.cmd文件,您必须输入filename.cmd。

#7


10  

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文件也有这种区别:如果您曾经创建过文件测试。蝙蝠和测试。在同一个目录下的CMD,在那个目录中运行测试,它将运行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>

#8


8  

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解释器执行的,因此在NTVDM仿真的16位环境下,应该更快(在短文件中不明显)和stabler。

#9


3  

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

我相信如果您将ComSpec环境变量的值更改为%SystemRoot%system32\cmd。如果文件扩展名是. bat或. cmd,那就无关紧要了。我不确定,但这可能是WinXP和上面的默认设置。

#10


3  

Slightly off topic, but have you considered Windows Scripting Host? You might find it nicer.

稍微偏离主题,但是您考虑过Windows脚本主机吗?你可能会觉得它更好。

#11


2  

The extension makes no difference. There are slight differences between COMMAND.COM handling the file vs. CMD.EXE

扩展没有区别。命令之间有细微的差别。处理文件与CMD.EXE。

#12


2  

.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变量中,它可以更改受命令扩展影响的命令。这是真的。

#13


-2  

Here is one difference I discovered: EnableDelayedExpansion is required in .cmd files.
Where as in case of .bat files it is implicit by default. (Windows 10)

这里有一个我发现的区别:EnableDelayedExpansion需要在.cmd文件中。如果是。bat文件,默认情况下是隐式的。(Windows 10)

dir *? | find /i "FOOBAR"
if ERRORLEVEL 0             (
set result="found"  ) else  (
set result="not found"  )
echo %result%

This works in .bat but is always found in case of a .cmd file.
Changing line 2 to the following makes it work as expected:

这在.bat中有效,但总是在.cmd文件中找到。将第2行改为如下所示:

if %ERRORLEVEL% equ 0       (

And finally for the .cmd file this works correctly:

最后,对于。cmd文件,这是正确的:

setLocal EnableDelayedExpansion
...
if !ErrorLevel! equ 1       (
...

#14


-2  

As being a Cmd programmer, and looking all over the web, it really doesn't matter which one you use, you can have a .bat program on a Windows 7, and run it on a Windows 10. but if you are to make it on a Windows 10, you probably won't be able to run all the commands on a Windows 7. A .cmd is the exact same way, and runs the exact same program and codes.

作为一名Cmd程序员,在网络上到处寻找,不管你使用哪一个,你都可以在Windows 7上拥有一个.bat程序,并在Windows 10上运行它。但是如果你要在Windows 10上运行,你很可能无法在Windows 7上运行所有的命令。一个。cmd是完全相同的方式,并且运行完全相同的程序和代码。

All the difference is, is that its a different name of the same program, as long as it is connected to CMD.EXE, it runs the same commands.

不同之处在于,它是同一个程序的不同名称,只要它与CMD连接。EXE,它运行相同的命令。

#15


-10  

a difference:

一个区别:

.cmd files are loaded into memory before being executed. .bat files execute a line, read the next line, execute that line...

.cmd文件在执行之前被加载到内存中。.bat文件执行一行,读取下一行,执行该行…

you can come across this when you execute a script file and then edit it before it's done executing. bat files will be messed up by this, but cmd files won't.

当您执行一个脚本文件并在它执行之前编辑它时,您会遇到这个问题。bat文件将会被这个问题搞砸,但是cmd文件不会。