Windows batch files: .bat vs .cmd?

As I understand it, .bat is the old 16-bit naming convention, and .cmd is for 32-bit Windows, ie, 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?


From this news group posting by Mark Zbikowski himself:

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.


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

  • command.com is the 16-bit command processor introduced in MS-DOS and was also used in the Win9x series of operating systems.
  • 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.
  • The ComSpec env variable defines which program is launched by .bat and .cmd scripts. (Starting with WinNT this defaults to cmd.exe .)
  • cmd.exe is backward compatible with command.com .
  • 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.
  • Here is a list of cmd.exe features that are not supported by 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
  • 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.

    References:

  • cmd.exe
  • command.com
  • wikipedia: Comparison of command shells


    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 enables built-in commands to change Errorlevel to 0 on success.
  • Edit: Command Extensions are on by default in both .bat and .cmd files under Windows 2000 or later.

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

    链接地址: http://www.djcxy.com/p/16436.html

    上一篇: Windows等同于UNIX pwd

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