Differences Between Windows Batch .bat and .cmd Files

Siddharth Bishnoi Jan 30, 2023
  1. Differences Between COMMAND.COM and cmd.exe
  2. Key Differences Between Windows Batch .bat and .cmd Files
  3. Conclusion
Differences Between Windows Batch .bat and .cmd Files

Users often consider COMMAND.COM and cmd.exe the same. Also, they are confused about which extension to use for the Batch files, .bat or .cmd.

Are they the same or different, and does it matter which extensions to use? The answer to all of these questions is discussed in this article.

When we run Batch files, they are executed in the Command Prompt. The Batch files are mostly run in cmd.exe in versions of Windows NT.

In older versions, it will run in the COMMAND.COM shell.

Differences Between COMMAND.COM and cmd.exe

COMMAND.COM and cmd.exe are versions of the command interpreter for MS-DOS and Windows. COMMAND.COM is a command-line shell designed to run 16-bit programs; it is a DOS program where it is the first to run after boot which sets up the system by running the AUTOEXEC.BAT configuration file.

It was initially released in 1980 for MS-DOS, Windows 95-98, and Windows ME. It is only compatible and useful for older programs designed to run as 16-bit programs and has not been updated since then.

command.com

The cmd.exe, on the other hand, is a successor of COMMAND.COM. It is designed for 32-bit programs and was released in 1987 for OS/2, Windows NT, and Windows CE.

Compared to COMMAND.COM, cmd.exe provides better extensions such as detailed error messages, supports arrow keys to check the command history, supports delayed variable expansion, and has improved internal commands.

cmd.exe

A Batch file is a script file that can store commands and execute them in serial order by the command-line interpreter. The .bat filename extension is used in MS-DOS and Windows, and it was the first extension used for the Batch files, whereas .cmd is used in the Windows NT family and OS/2.

.cmd files can only be executed in cmd.exe as the COMMAND.COM does not recognize this extension.

This tutorial will illustrate the key differences between .bat and .cmd files.

Key Differences Between Windows Batch .bat and .cmd Files

The essential key differences between .bat and .cmd are discussed below.

ERRORLEVEL Handling

The .bat and .cmd handle the ERRORLEVEL variable differently. When an ERRORLEVEL is called, .bat changes its state only when a real error occurs, i.e., for commands such as append, dpath, ftype, set, path, assoc, .bat will not set the ERRORLEVEL.

But, in the case of .cmd, it will set the ERRORLEVEL regardless of a real error. It will reset the ERRORLEVEL to 0 even after successful execution.

Sometimes, it creates confusion while writing a script, so users should be careful of this.

Order of Execution

An environment variable PATHEXT controls the execution order. The execution depends on the order of the variables stored in the PATHEXT variable.

If both .bat and .cmd scripts are stored in the same folder, executing the script without extension will run the .bat file first, followed by the .cmd file. The default order of the PATHEXT variable is as follows:

ECHO %PATHEXT%

pathext variable

However, you can change the order of the extensions in the PATHEXT variable. Even if you delete the PATHEXT variable, the order will be the same as the MS-DOS version of Windows.

Support

Regarding support for Windows versions, .cmd files are supported in almost all Windows versions which are not the same as in the case of .bat files. .bat was initially developed for MS-DOS, while .cmd was developed for Windows NT, so .cmd files do not execute in non-Windows NT systems.

The scripting for .bat is older as compared to .cmd. And .cmd is backward compatible when compared to .bat files.

Security

Regarding security, .cmd is safer than .bat. Because commands in a .bat file are stored in a serial manner, and it executes commands line by line, it is more prone to security attacks. .cmd files do not execute commands sequentially, making them safer than .bat files.

.cmd files can also be used in Android through an application that allows to view, install, and run .cmd files. .bat files are not supported in Android.

Language

.bat files have an older version of Microsoft language as it was developed earlier for MS-DOS. .cmd is a programming language and interpreter developed for the newer versions of Windows.

When you run a Batch file without any extension, it will run as .bat whereas, for the .cmd file extension, you must enter the file name as filename.cmd. This case applies only when both the extensions have the same filename; .cmd needs to provide extensions to run the files while .bat files directly run the program without needing extensions.

As shown in the image below, we have two Batch files with the same filename testfile and different extensions .bat and .cmd. The .bat file is stored as Windows Batch File and .cmd as Windows Command Script.

batch files with the same filename

Running the .bat file:

running bat file

Running the .cmd file:

running cmd file

Working Environment

The .bat files were designed to run in an NTVDM environment, a Windows process to run 16-bit programs on 32-bit Windows. NTVDM stands for Windows NT Virtual DOS Machine, executed as ntvdm.exe.

It is an important program for the system, which should not be removed for the stable running of programs. The .bat files run in this environment and are therefore supported in all environments, whereas .cmd files cannot execute in 16-bit environmentS.

Also, .cmd files are stored in memory before the execution, whereas the .bat files execute commands line by line without storing them in memory.

Interpreter

Batch files with the .bat file extension can be run under both COMMAND.COM and cmd.exe, though both the interpreters will execute the file differently. The .cmd files only run in cmd.exe.

The COMSPEC environment variable is used to launch the command line interpreter for Batch files. By default, it is set to COMMAND.COM for older versions such as MS-DOS.

Starting from Windows NT, it defaults to cmd.exe. To view its content, execute the following command:

ECHO %COMSPEC%

compspec variable

So, newer versions of Windows will run in cmd.exe. But in older versions, only .bat files will be executed.

Conclusion

So, we have discussed the essential key differences between batch file extensions .bat and .cmd. Both have their advantages over one another.

To conclude, everything that works in a .bat file should also work in a .cmd file. The .cmd files are faster and more stable than .bat files.

Using the right extension depends on your requirements. But, it is recommended to use the .cmd file extension for newer versions.

Related Article - Batch CMD