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 anyhting 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?
views:
17396answers:
8No - 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.
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):
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.
From Wikipedia:
New Quote from Wikipedia
The only known difference between .cmd and .bat file execution is that in a .cmd file the ERRORLEVEL variable changes even on a successful command that is affected by Command Extensions (when Command Extensions are enabled), whereas in .bat files the ERRORLEVEL variable changes only upon errors.
The extension makes no difference. There are slight differences between COMMAND.COM handling the file vs. CMD.EXE
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
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.
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 introduced in Windows NT, (64-bit Windows OS's also have a 64-bit version).cmd.exe
was never part of Win9x.- The
ComSpec
env variable defines which program is launched by.bat
and.cmd
scripts. (Starting with WinNT this defaults tocmd.exe
.) cmd.exe
is backward compatible withcommand.com
.- A script that is designed for
cmd.exe
can be named.cmd
to prevent accidental execution on Windows 9x.
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:
wikipedia: Comparison of command shells
Slightly off topic, but have you considered Windows Scripting Host? You might find it nicer.
a difference:
.cmd files are loaded into memory before being executed. .bat files execute a line, read the next line, execute that line...
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.