Batch file
From Wikipedia, the free encyclopedia
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. Linux has a similar type of file called a Shell Script.
DOS batch files have the filename extension .bat
(or .BAT
because file names are case insensitive in DOS, Windows and OS/2). Batch files for other environments may have different extensions, e.g. .cmd
in Windows NT and OS/2, or .btm
in 4DOS and related shells. The Windows 9x family only recognizes the .bat extension.
Contents |
[edit] History
The command interpreters provide two distinct modes of work: interactive mode (in which the user types commands at a command prompt which are then executed immediately) and batch mode (which executes a predefined sequence of commands). The original concepts for both modes draw ideas from 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.
Microsoft DOS and Windows batch programming has evolved along with the product releases of these operating systems. Although a batch file is analogous to a shell script in Unix-like operating systems, the limited syntax and commands available means it is less suited for general-purpose programming. These limitations 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) and in 1989 the replacement shell 4DOS and later 4NT.
[edit] DOS
The batch program interpreter for MS-DOS and compatible DOSes is COMMAND.COM
. Batch programs for MS-DOS are composed of a relatively simple set of commands interpreted directly by COMMAND.COM
(internal commands), and utilities that exist as separate executables (external commands). The most commonly used batch file was AUTOEXEC.BAT
. If present, it was automatically executed during the booting process.
The evolution of this branch of batch programming proceeded through the releases of MS-DOS, and continued with the early versions of Microsoft Windows through to Windows 95, Windows 98, and finally Windows Me, which remained based on MS-DOS.
[edit] Modern Windows
Modern versions of Microsoft Windows such as Windows XP and Windows Vista are not based on MS-DOS, but on Windows NT. NT-based systems's native command line interpreter is CMD.EXE
which, while generally compatible with MS-DOS, provides many additional features and commands. (The older COMMAND.COM
of MS-DOS is still available on NT-based systems where necessary for better backward compatibility.) It is a common myth that .bat are run by COMMAND.COM while .cmd are run by CMD.EXE. Nevertheless, there are differences between the .bat and .cmd extensions which affect when errorlevels are set.[1]
For complex tasks in Windows 98 and up the Windows Script Host may be used. It allows the running of scripts written in VBScript, JScript and related scripting languages, but is more akin to traditional programming languages. It has no command line interface.
In 2006, Microsoft created another scripting tool, Windows PowerShell, which can be used with Windows XP and above. It is also designed for interactive use from command line interface.
A modern way of securely distributing batch files is to compile them to executables. Some BAT to EXE compilers encrypt the scripts so they cannot be viewed with a normal text editor.
[edit] OS/2
Although the IBM OS/2 operating system supported DOS-style batch files, it also included a version of REXX — a more advanced scripting language.
[edit] Example
An example of a simple batch file:
@echo off cls echo Hello World! echo. pause
This batch file would first turn off 'echo'. This only displays the output of the command on the screen, not the input (EXAMPLE: Input: pause --- Output: Press Any Key To Continue.). Then, it would clear the screen after the echo off command (cls
), then it would display a line of text "Hello World!" (echo Hello World!
), then an empty line below it (echo.
), then it would keep the screen open until the user presses any key with the message "Press any key to continue. . . ." (pause
).
[edit] See also
Wikibooks has a book on the topic of |
- VBScript
- Windows PowerShell, extensible command-line shell released in 2006
- List of DOS commands