It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file.
Similar to Job Control Language (JCL), DCL and other systems on mainframe and minicomputer systems, batch files were added to ease the work required for certain regular tasks by allowing the user to set up a script to automate them.
[1] Unix-like operating systems, such as Linux, have a similar, but more flexible, type of file called a shell script.
In MS-DOS, a batch file can be started from the command-line interface by typing its name, followed by any required parameters and pressing the ↵ Enter key.
Microsoft Windows was introduced in 1985 as a graphical user interface-based (GUI) overlay on text-based operating systems and was designed to run on DOS.
In order to start it, the WIN command was used, which could be added to the end of the AUTOEXEC.BAT file to allow automatic loading of Windows.
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 DOS-like console shell without mention of Microsoft, naming it just DOS, although this seemingly made no difference with regard to the way batch files worked from COMMAND.COM.
Windows NT introduced an enhanced 32-bit command interpreter (cmd.exe) that could execute scripts with either the .CMD or .BAT extension.
[6] This example batch file displays Hello World!, prompts and waits for the user to press a key, and then terminates.
The combined @ECHO OFF is often as here the first line of a batch file, preventing any commands from displaying, itself included.
For example, if %foo% is empty, this statement: parses as the erroneous construct: Similarly, if %foo% contains abc def, then a different syntax error results: The usual way to prevent this problem is to surround variable expansions in quotes so that an empty variable expands into the valid expression IF ""=="bar" instead of the invalid IF ==bar.
Example: Batch interpreters prior to Windows 2000 would have displayed result MyVar is not defined.
As a result, each Windows application can implement its own parser to split the entire command line into arguments.
Many applications and command-line tools have evolved their own syntax for doing that, and so there is no single convention for quoting or escaping metacharacters on Windows command lines.
It is necessary to escape both the caret itself and the escaped character for the character display as text in the variable: The delayed expansion available with or with in Windows 2000 and later may be used to show special characters stored in environment variables because the variable value is expanded after the command was parsed: Until the TIMEOUT command was introduced with Windows Vista, there was no easy way to implement a timed pause, as the PAUSE command halts script activity indefinitely until any key is pressed.
Many workarounds were possible,[10] but generally only worked in some environments: The CHOICE command was not available in older DOS versions, PING was only available if TCP/IP was installed, and so on.
It does not matter if the two echo commands share the same command line; the CR/LF codes are inserted to break the output onto separate lines: A trick discovered with Windows 2000 and later is to use the special prompt for input to output text without CR/LF trailing the text.
It is not possible to have a command prompt that uses a UNC path as the current working directory; e.g. \\server\share\directory\ The command prompt requires the use of drive letters to assign a working directory, which makes running complex batch files stored on a server UNC share more difficult.
This also solves a problem related to User Account Control (UAC) on Windows Vista and newer.
[13] Non-English file names work only if entered through a DOS character set compatible editor.
For example, cmd /U /C dir > files.txt creates a file containing a directory listing with correct Windows characters, in the UTF-16LE encoding.