Batch File - Wikipedia
Batch File - Wikipedia
Batch file
A batch file is a kind of Batch file
script file in DOS, OS/2
and Microsoft Windows. It
consists of a series of
commands to be executed
by the command-line Filename
.bat, .cmd, .btm
extensions
interpreter, stored in a
plain text file. A batch file application/bat
may contain any command
application/x-bat
the interpreter accepts Internet
application/x-msdos-
interactively and use media type
program
constructs that enable
text/plain
conditional branching and
looping within the batch Type of format Scripting
file, such as IF, FOR, and
Container for Scripts
GOTO labels. The term
"batch" is from batch processing, meaning "non-interactive execution",
though a batch file may not process a batch of multiple data.
The detailed handling of batch files has changed. Some of the detail in this
article applies to all batch files, while other details apply only to certain
versions.
Variants
DOS
A .bat file name extension identifies a file containing commands that are
executed by the command interpreter COMMAND.COM line by line, as if it
were a list of commands entered manually, with some extra batch-file-
specific commands for basic programming functionality, including a GOTO
command for changing flow of line execution.
Early Windows
OS/2
The IBM OS/2 operating system supported DOS-style batch files. It also
included a version of REXX, a more advanced batch-file 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 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
Filename extensions
.bat
The first filename extension used by Microsoft for batch files. This
extension runs with DOS and all versions of Windows, under
COMMAND.COM or cmd.exe, despite the different ways the two
command interpreters execute batch files.
.cmd
Used for batch files in Windows NT family and sent to cmd.exe for
interpretation. COMMAND.COM does not recognize this file name
extension, so cmd.exe scripts are not executed in the wrong Windows
environment by mistake. In addition, append, dpath, ftype, set, path,
assoc and prompt commands, when executed from a .bat file, alter the
value of the errorlevel variable only upon an error, whereas from
within a .cmd file, they would affect errorlevel even when returning
without an error.[3] It is also used by IBM's OS/2 for batch files.
.btm
The extension used by 4DOS, 4OS2, 4NT and Take Command. These
scripts are faster, especially with longer ones, as the script is loaded
entirely ready for execution, rather than line-by-line.[4]
Examples
This example batch file displays Hello World!, prompts and waits for the
user to press a key, and then terminates. (Note: It does not matter if
commands are lowercase or uppercase unless working with variables)
@ECHO OFF
ECHO Hello World!
PAUSE
To execute the file, it must be saved with the extension .bat (or .cmd for
Windows NT-type operating systems) in plain text format, typically created
by using a text editor such as Microsoft Notepad or a word processor in
text mode.
Hello World!
Press any key to continue . . .
Explanation
The interpreter executes each line in turn, starting with the first. The @
symbol at the start of any line prevents the prompt from displaying that
command as it is executed. The command ECHO OFF turns off the prompt
permanently, or until it is turned on again. The combined @ECHO OFF is
often as here the first line of a batch file, preventing any commands from
displaying, itself included. Then the next line is executed and the ECHO
Hello World! command outputs Hello World!. The next line is executed
and the PAUSE command displays Press any key to continue . . . and
pauses the script's execution. After a key is pressed, the script terminates,
as there are no more commands. In Windows, if the script is executed from
an already running command prompt window, the window remains open
at the prompt as in MS-DOS; otherwise, the window closes on termination.
Variable expansions are substituted textually into the command, and thus
variables which contain nothing simply disappear from the syntax, and
variables which contain spaces turn into multiple tokens. This can lead to
syntax errors or bugs.
Similarly, if %foo% contains abc def, then a different syntax error results:
C:\>set MyVar=
C:\>echo %MyVar%
%MyVar%
C:\>if "%MyVar%"=="" (echo MyVar is not defined) else (echo MyVar is %MyVar%
MyVar is %MyVar%
This can cause conflicts where a string contains quotation marks, and is to
be inserted into another line of text that must also be enclosed in quotation
marks:
C:\>set foo=%foo:"="""%
Some characters, such as pipe (|) characters, have special meaning to the
command line. They cannot be printed as text using the ECHO command
unless escaped using the caret ^ symbol:
However, escaping does not work as expected when inserting the escaped
character into an environment variable. The variable ends up containing a
live pipe command when merely echoed. It is necessary to escape both the
caret itself and the escaped character for the character display as text in the
variable:
C:\>cmd /V:ON
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
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.
Normally, all printed text automatically has the control characters for
carriage return (CR) and line feed (LF) appended to the end of each line.
batchtest.bat
C:\>batchtest.bat
foo
bar
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. In this
example, the CR/LF does not follow Message 1, but does follow Line 2 and
Line 3:
batchtest2.bat
@echo off
set /p ="Message 1"<nul
echo Message 2
echo Message 3
C:\>batchtest2.bat
Message 1Message 2
Message 3
This can be used to output data to a text file without CR/LF appended to
the end:
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. While a batch file can be run from a UNC file
path, the working directory default is C:\Windows\System32\.
In Windows 2000 and later, a workaround is to use the PUSHD and POPD
command with command extensions.[9]
So to run a batch file on a UNC share, assign a temporary drive letter to the
UNC share, and use the UNC share as the working directory of the batch
file, a Windows shortcut can be constructed that looks like this:
enabled, and they try to run a batch file as administrator from a network
drive letter, using the right-click file context menu, the operation will
unexpectedly fail. This is because the elevated UAC privileged account
context does not have network drive letter assignments, and it is not
possible to assign drive letters for the elevated context via the Explorer
shell or logon scripts. However, by creating a shortcut to the batch file
using the above PUSHD / POPD construct, and using the shortcut to run
the batch file as administrator, the temporary drive letter will be created
and removed in the elevated account context, and the batch file will
function correctly.
The following syntax does correctly expand to the path of the current batch
script.
%~dp0
UNC default paths are turned off by default as they used to crash older
programs.[10]
Character set
Batch files use an OEM character set, as defined by the computer, e.g. Code
page 437. The non-ASCII parts of these are incompatible with the Unicode
or Windows character sets otherwise used in Windows so care needs to be
taken.[11] Non-English file names work only if entered through a DOS
character set compatible editor. File names with characters outside this set
To get output in Unicode into file pipes from an internal command such as
dir, one can use the cmd /U command. For example, cmd /U /C dir >
files.txt creates a file containing a directory listing with correct Windows
characters, in the UTF-16LE encoding.
The following command in a batch file will delete all the data in the current
directory (folder) - without first asking for confirmation:
These three commands are a simple fork bomb that will continually
replicate itself to deplete available system resources, slowing down or
crashing the system:
:TOP
start "" %0
goto TOP
There are other, later and more powerful, scripting languages available for
Windows. However, these require the scripting language interpreter to be
Script files run if the filename without extension is entered. There are rules
of precedence governing interpretation of, say, DoThis if DoThis.cmd,
DoThis.bat, DoThis.exe, etc. exist; by default DoThis.cmd has highest
priority. This default order may be modified in newer operating systems by
the user-settable PATHEXT environment variable.
See also
References
External links