views:

17396

answers:

8

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?

+13  A: 

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.

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.

Michael Burr
+24  A: 

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.

Ben Hoffstein
Okay, COMMAND.com vs CMD.com. So what practical differences result from one processor running my script vs the other?
Chris Noe
On my system, cmd.exe executes .bat files even if started by a shortcut (WinXP SP2).
Michael Burr
So it's sounding like Microsoft has effectively made the two suffixes into synonyms. In which case I wonder why command.com is still lying around in the windows directory?
Chris Noe
With a bazillion customers, I'm sure there are people who still need the compatibility. ".bat" files will run under command.com if they are spawned by a 16-bit process.
Michael Burr
If .bat files do indeed run the 16-bit COMMAND.COM, an issue could be that 16-bit programs don't run on Win64.
Chris Smith
If anyone knows how Vista runs batch files, I think it would be worth writing a separate answer about it..
Chris Noe
Wikipedia is wrong. Only cmd.exe is used to run batch files, whether they are .cmd or .bat. Vista runs batch files under cmd.exe in exactly the same manner as xp.
Wedge
Wedge is correct; the %cmdcmdline% variable contains the name of the process used to run the script. From both a command console and from Explorer "cmd.exe" is used.
Patrick Cuff
Yup. This answer is *wrong*, except for the first sentence. See the answer by Chris Noe.
Mark
-1 for (in large part) wrong answer. See Wedge's arguments.
pipitas
+1  A: 

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

Waldo
+5  A: 

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

Lorenzo Boccaccia
+1  A: 

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.

Patrick Cuff
+61  A: 

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. 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.
  3. The ComSpec env variable defines which program is launched by .bat and .cmd scripts. (Starting with WinNT this defaults to cmd.exe.)
  4. cmd.exe is backward compatible with command.com.
  5. 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

Chris Noe
Several minor points: 1) .bat does not necessarily invoke command.com - apparently when command.com is invoked is a bit of a complex mystery; 2) command.com was introduced with MS-DOS; 3) cmd.exe can run most command.com scripts, but there are a few minor command.com things that don't work in cmd.
Michael Burr
Hey Mike, if you have any specifics on command.com features that don't work on cmd.exe, I think it would be worth posting a separate answer on that.
Chris Noe
It's been so long since I worked on Win9x or DOS the only thing I can remember is that you could do something like "cd ..." to go up more than one directory level (hey, I said minor, didn't I?). However, see my edited answer for info about command.com's processing on NT.
Michael Burr
cmd.exe was introduced with NT 4.0 I believe, not windows 95.
FlySwat
Chris: see the current version of the Wikipedia article, esp. the comment by Mark Zbikowski at http://groups.google.com/group/microsoft.public.win2000.cmdprompt.admin/msg/ad9066638815812c
Mark
Using command.com you can load ansi.sys to enable ANSI escape codes for coloring text. I'm not sure if you can load ansi.sys in cmd.exe.
molasses
@molasses: You can't. With 64-bit systems it's a moot point now, though.
Joey
A: 

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

Marcin
For that matter, PowerShell, which deprecates WSH/cscript.exe.
stimpy77
A: 

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.

grey
As has been established, the ComSpec env variable defines which program is launched, are you essentially saying that command.com reads the file a line at a time, while cmd.exe pre-loads the file into memory? Can you cite a reference on this?
Chris Noe