Batch file

Jump to: navigation, search
Batch file
Filename extension .bat .cmd .btm
Type of format Scripting
Container for Shell scripts

In DOS, OS/2, and Microsoft Windows, a batch file is a text file containing a series of commands intended to be executed by the command interpreter. When a batch file is run, the shell program (usually COMMAND.COM or cmd.exe) reads the file and executes its commands, normally line-by-line. Batch files are useful for running a sequence of executables automatically and are often used by system administrators to automate tedious processes.[1] Unix-like operating systems (such as Linux) have a similar type of file called a shell script.[2]

DOS batch files have the filename extension .bat. Batch files for other environments may have different extensions, e.g. .cmd or .bat in the Microsoft Windows NT-family of operating systems and OS/2, or .btm in 4DOS and related shells. The now-obsolete Windows 9x family of operating systems only recognize the .bat extension.


Originally command interpreters' primary responsibility was to execute out commands entered manually. Such commands might involve starting programs, carrying out file operations, executing functions concerned with controlling the system, setting preferences or administrative tasks. Sequences of such commands were also sometimes stored in files, which could be later passed to the command interpreter to be read and executed, so such stored sequences could be termed sets of 'batch commands'.

Over time, command interpreters or 'shells' grew additional features, as such stored sequences of such 'batch commands' became more complex, and command interpreters evolved into something more akin to interpreters for a kind of limited programming languages or 'script'. Additional commands, advanced syntactic features and computation abilities were added which allowed sophisticated programs to be written so that batch files or scripts could contain a mixture of commands of the traditional.

Early influences

Microsoft DOS batch language was influenced by various Unix shells, as well as other text-based command line interfaces from the early 1980s such as CP/M which in turn took much of their inspiration from TOPS-10 and TOPS-20 from Digital Equipment Corporation. Although a DOS batch file is analogous to a shell script in Unix-like operating systems, the syntax and range of commands available is less sophisticated.


The first example of Microsoft's batch files were the .bat files used by DOS. The operating system used COMMAND.COM to process commands and batch files. Commands were either internal (part of COMMAND.COM), or if they were too large to keep in the main file; external, where COMMAND.COM would look for the command each time it is requested at the prompt (or display an error message if it didn't exist). This meant that if one wanted, they could add commands to DOS, and in turn allow more functionality to batch files when using the new commands.

An example of an important batch file was AUTOEXEC.BAT which automatically runs after DOS loads during booting. It typically had commands to load drivers.[2]

Enhancements and alternatives

The limitations of the DOS command intrepreter led to various non-Microsoft interpreters to provide enhanced syntax by providing "enhancement" commands such as those in the Norton Utilities (like the BE or Batch Enhancer), in 1989 the replacement shell 4DOS.

Early Windows

Microsoft Windows was introduced in 1985 as a GUI Operating System alternative to text-based operating and was designed to run on MS-DOS. In order to start it the WIN command was used and could be added to the end of the AUTOEXEC.BAT file to allow automatic loading of Windows. In the earlier versions one could run a .bat type file from Windows in the MS-DOS Prompt.

Windows was based on MS-DOS and used COMMAND.COM to run .bat files on the following operating systems:

  • Windows 1, 2 and 3.
  • Windows 95 and 98.
  • Windows ME (access to real mode MS-DOS was restricted).


The IBM OS/2 operating system supported DOS-style batch files. It also included a version of REXX, which was a more advanced scripting language. IBM and Microsoft started developing this system but during the construction of it broke up after a dispute, as a result of this IBM referred to their MS-DOS like console shell without mention of Microsoft; naming it just DOS, although this seemingly had no impact on the way batch files worked.

Windows NT

The Microsoft Windows NT-family of operating systems featured a second, new command interpreter cmd.exe. CMD was far more sophisticated than it's DOS counterpart, the old COMMAND.COM interpreter which was also retained but launched inside an MS-DOS virtual machine emulator facility. CMD batch files may have extensions either .cmd or .bat.

Filename Extensions

  • .bat: The first extension used by Microsoft for batch files. This extension can be run in most Microsoft Operating Systems, including DOS and most versions of Microsoft Windows.
  • .cmd: The newer .cmd extension is described by Windows NT-family 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).[3]
  • .btm: The .btm file extension is used by 4DOS rather than Command Prompt or COMMAND.COM. This non-Microsoft Filename extension when used with larger scripts is faster than .cmd or .bat scripts because it is all loaded at once into one command, rather than line-by-line (as it is done with .bat and .cmd extensions).[4]


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.[5]


ECHO Hello World!

To execute the file it must be saved with a .bat or .cmd extension in plain text format (with a program like Notepad).


When executed (either from Windows Explorer or Command Prompt) this is displayed:

Hello World!
Press any key to continue . . .


Batch files are executed by every line being executed in order until the end is reached or something else stops it (such as the key shortcut for terminating batch processing; 'Ctrl' + 'C'). This batch file first turns off the 'echo' with ECHO OFF. This stops the display of input from the batch file and limits the display to output from commands only. Since this command is executed before the input is silenced, the @ symbol is used at the start of the command which stops that command line showing input. Then the ECHO command is used again in the form ECHO.Hello World! which outputs the line Hello World!. The . is important as it stops the ECHO command confusing an attempt to output a line with 'ON' or 'OFF' at the start with an attempt to change the state of showing input, and it is easier to tell which lines are outputs with the . there as a visual aid. Then the command ECHO. is used which adds the empty line below Hello World!, again using the . so that the command doesn't output the input display's state (ECHO is on. or ECHO is off.) and just outputs an empty line. Then the PAUSE command is used which pauses execution until the user presses a key. The Press any key to continue . . . prompt is output by the command. Lastly, after the user presses a key the command ECHO ON is used which turns the prompt and input on again, so that if the file is executed from the Command Prompt, rather than Windows Explorer then when the execution ends the user can see the prompt again to use normally. After the last line is reached the batch file ends execution automatically. If it was started from the command prompt (by entering the name of the file when in its directory) then the window remains when finished, but when started from Windows Explorer the window automatically closes upon the end of execution.


Microsoft hasn't officially released information pertaining to the future of Command Prompt (host for .bat and .cmd files) yet, but the company is now starting to include Windows PowerShell in releases for newer Operating Systems, which has all the core functions of Command Prompt and more (and instead of .bat and .cmd files, it runs .ps1 files). Yet it is important to remember that it is not certain this will replace Command Prompt, and that Microsoft is still making important tools for Command Prompt specifically, instead of for PowerShell (such as cmd.exe, which incorporates the entire set of Server Manager functions for Windows Server 2008).[6]

Other raw-text scripting files

Batch files aren't the only text-based scripting files, and are in no way the most powerful and complex. Needs for more powerful capabilities led to the development of these new text-based scripting files:

  • .kix: Started in 1991, the need for commands useful in a network logon script led to the development of KiXtart by a Microsoft employee which kept the 'feel' of the traditional batch file.
  • .vbs and .js: Released in 1998, Windows Script Host (comprised of cscript.exe and wscript.exe) allows the running of scripts written in VBScript or JScript. They can be run in windowed mode (with the wscript.exe host) and console-based mode (with the cscript.exe host). They were included as a part of Windows since Windows 98.
  • .ps1: In 2006, Microsoft released a further raw-text script processor, Windows PowerShell, which can be used with Windows XP (SP2/SP3) and above. This is designed for both interactive use from a command line interface, and also for writing scripts, and has a strong resemblance to Unix shells.[7]

See also


External links