Batch file
From Wikipedia, the free encyclopedia
- This article is about MS-DOS scripting. For other types of batch programming, see shell script.
File extension: | .bat, .cmd (Windows NT only) |
---|---|
Type of format: | Scripting |
In MS-DOS, OS/2 and 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. A batch file is analogous to a shell script in Unix-like operating systems.
DOS batch files have the filename extension .BAT
. 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.
AUTOEXEC.BAT
is a special batch file that is executed during the booting process.
Contents |
[edit] History
Microsoft DOS and Windows batch programming has evolved along with the product releases of these operating systems. The command interpreters that come with these operating systems provide two distinct modes of work: interactive mode (in which the user types commands at a 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.
The MS-DOS operating system's batch program interpreter 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 evolution of this branch of batch programming proceeded through the releases of MS-DOS, and into Windows 95, Windows 98, and finally Windows Me.
The newest versions of Windows, Windows 2000 and XP, are not based on MS-DOS, but on Windows NT. NT-based systems include the cmd.exe
command-line interpreter, which is somewhat compatible with COMMAND.COM
. Some MS-DOS features are not available, but there are many additional features and commands not included with MS-DOS or MS-DOS-based versions of Windows. COMMAND.COM
is still present under Windows NT operating systems for better backward compatibility.
Various non-Microsoft command interpreters exist that provide enhanced batch program command syntax. An example of these is the 4DOS product.
Several non-Microsoft implementations of batch compilers exist to convert batch programs to directly executable programs. The quality-of-implementation of these compilers varies widely.
The IBM OS/2 operating system contains a text based command facility that is related to the ones supplied with Microsoft operating systems. However, OS/2 evolution assumed replacement of .BAT files to REXX scripting.
[edit] @echo off
The default behaviour of the shell is to print each command to standard output before executing it. This is usually not desired, so the command echo off
is given at the beginning of a batch file to prevent that from happening. In that form, only the echo off
command itself would be printed. Since individual commands may be prefixed with the @
character to be prevented from printing, most batch files start with the line @echo off
, so that echo off
is not displayed, and nor are any other commands in the file.
[edit] Example
An example of a simple batch file:
@echo off cls echo. echo Hello World, press any key to start AProgram.exe! pause > nul AProgram.exe echo. echo AProgram has finished whatever it was doing. Have fun today!
Note:The echo. command may be used in recent versions to output a completely blank line.
[edit] Uses
Batch files are extremely useful for executing numerous commands automatically. Many administrators use them to automate tedious processes.
They are mainly used to use the "set" command, which sets a string to another, permanently.
Students in public schools connected to LAN usually abuse it to create pranks (message floods, etc.).
(an example of a prank is %windir%\system32\shutdown.exe -r -t 00 To use it, copy it into notepad and save the file with a .bat file extension. Cause havoc by sending it as an email attachment!)