What's the difference between .cmd and .bat files?

Just curious. "Cool" people in our company always use *.cmd while no one was able to explain the difference to me.


Here is a good discussion from Stackoverflow.


There are semantic differences in the command language used for each (.bat files get a compatibility version). Some of these can be illustrated by this script from over here:

@echo off&setlocal ENABLEEXTENSIONS
call :func&&echo/I'm a cmd||echo/I'm a bat
goto :EOF

:func
md;2>nul
set var=1

Basically, .cmd files get the current, sexier version of the MS command language, which is why the cool kids use them.


According to Wikipedia:

.bat: The first extension used by Microsoft for batch files. This extension can be run in most Microsoft Operating Systems, including MS-DOS and most versions of Microsoft Windows.

.cmd: The newer .cmd extension is described by Windows NT based systems as a 'Windows NT Command Script' and is helpful, as using a .cmd extension rather than .bat extension means that earlier versions of Windows won't know how to run it, so that they don't try to and mistake the commands for COMMAND.COM style files and fail to run the new style commands due to the lack of command extensions, resulting in scripts only being partially run which could prove damaging (for example; failing to check the successful copying of a file and then deleting the original anyway).

The only known difference between .cmd and .bat file processing 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.

Hope this helps.