See also the pages on MS-DOS commands and
DOS variables.
A batch file is a sequence of DOS commands stored in a file with
extension ".BAT", which can be executed by typing the filename at the DOS
command prompt.
Any DOS command can appear in a batch file; the following commands are
particularly useful:
- rem text
A remark; all text on the line is ignored.
- echo off
Tells DOS not to display on the screen any commands following this
line. (This can be reversed by typing echo on.)
Note: If the first character on a line in the batch file is
"@", that line will be treated as if the echo off
command had been issued. If the first line of a batch file is
@echo off (and the echo on command is not
issued), no commands will be displayed on the screen.
- echo text
Displays the given text on the screen.
- echo.
Displays a blank line. (Note the period at the end of the command.
If you just type echo on a line by itself, DOS will
respond by reporting "ECHO is on" or "ECHO is off".)
- pause
Displays "Press any key to continue" and waits until a key is
pressed.
- goto label
Causes execution to continue with the line following
":label" (this label should be on a line all by
itself).
- call filename argument1 argument2 ...
Executes filename.bat, then continues on with the next line
of the current batch file. The command
filename argument1
argument2 ...
(without call) will also execute filename.bat, but
execution will not return to the current batch file. For example, suppose the file
batch1.bat contains the following lines, and batch2.bat is
another batch file.
echo This line is in batch1.bat.
rem Next line passes execution to batch2.bat
batch2
echo This line is also in batch1.bat
In this form, the echo command in line 4 would not be
executed. If call were placed at the beginning of the
third line, the fourth line would be executed after batch2.bat
was finished.
- for %%v in (string filespec ...) do
command
Execute command with %%v taking the value of each
string or filespec inside the parentheses. For example:
for %%v in (Bob Larry Junior) do echo My name is %%v.
would result in the following output:
My name is Bob.
My name is Larry.
My name is Junior.
As another example, the command:
for %%v in (*.bat *.txt) do type %%v
would "type" all the batch and text files in the current directory. (In fact, any
letter can be used in place of "v".)
- if string1==string2 command
Executes command if the two strings are equal or if the
strings are not equal, by simply adding the word "not", as in:
if not string1==string2 command
This can be used, for example, to check for options in a batch file. Suppose you
want to make a batch file foo.bat to perform some task, and you want it to
supply instructions if the command foo /? is entered. (This
is, after all, the way most DOS commands work.) This can be accomplished with the
following lines in foo.bat:
if not _%1==_/? goto continue
echo Usage: foo filename text
echo.
echo
Does some stuff to the specified file.
goto finished
:continue
rem Here are the actual commands ...
...
rem The following is the last line of the
file.
:finished
One comment about the very first line: The underscore "_" is included
on both sides of the equation to guarantee that the left side is not
blank. If foo was entered with no arguments,
if not %1==/? would reduce to if not
==/?, which would result in a syntax error.
- if exist filename command
(Also if not exist ....) Executes command
if filename exists (or does not exist).
- if errorlevel number command
(Also if not errorlevel ....) Some programs
and commands return an "exit code" a number that
indicates something about the results of executing that
command. if errorlevel number ...
executes command if the exit code returned by the
most recent command was at least as large as number. (The name
"errorlevel" is somewhat unfortunate, since this exit code does not
necessarily indicate that an error occurred.)
One useful example
is the command CHOICE, which allows a batch file to pause for user
input, and reports the results in its exit code. For more information about this
command, enter choice /?.