Batch Commands
A batch file or batch program is an unformatted text
file that contains one or more MS-DOS commands and is assigned a .BAT extension.
When you type the name of the batch program at the command prompt, the
commands are carried out as a group.
Any MS-DOS you use at the command prompt can also
be put in a batch program. In addition, the following MD-DOS commands are
specially designed for batch programs :
<Call>
<Choice>
<Echo>
<For>
<Goto>
<If>
<Pause>
<Rem>
<Shift>
You can use the COMMAND /Y command to step through
a batch program line by line, and can selectively bypass or carry out individual
commands. This is useful for tracking down problems in batch files. For
more information, see <COMMAND>.
CALL
Calls one batch program from another without causing
the first batch program to stop.
Syntax
CALL [drive:] [path] filename [batch-parameters]
Parameters
[drive:] [path] filename
Specifies the location and name of the batch program
you want to call.
Filename must have a .BAT extension
Batch-parameters
Specifies any command-line information required by
the batch program.
CALL - Notes
Using batch parameters
Batch-parameters can contain any information that
you can pass to a batch program, including switches, filenames, the replaceable
parameters %1 through %9, and variables such as %baud%.
Using pipes and redirection symbols.
Do not use pipes ("|") and redirection symbols ("<<",
"<", ">" and ">>") with the CALL command
Making a recursive call.
You can create a batch program that calls itself
; however, you must provide an exit command. Otherwise, the parent and
child batch programs can loop endlessy.
CALL - Examples
To run the CHECKNEW.BAT program
from another batch program, include the following command in the parent
batch program : call checknew
Suppose the parent batch program
accepts two replaceable parameters and you want it tp pass those parameters
to CHECKNEW.BAT. You can use the following command in the parent batch
program :
call checknew %1 %2
CHOICE
Prompts the user to make a
choice in a batch command. Displays a specified prompt and pauses for the
user to choose from among a specified set of keys. You can use this command
only in batch programs.
For more information on the
ERRORLEVEL parameter, see <CHOICE - Note> and the <IF> command.
Syntax
CHOICE [/C[:]keys] [/N] [/T[:]c,nn]
[text]
Parameters
text
Specifies the text you want
to be displayed before the prompt. Quotation marks are necessary only if
you include a switch character (/) as part of the text before the prompt.
If you don't specify text, CHOICE displays only a prompt.
Switches
/C [:]
Specifies allowable keys in
the prompt. When displayed, the keys will be separated by commas, will
appear in brackets ([]), and will be followed by a question mark. If you
don't specify the /C switch, CHOICE uses YN as the default. The colon (:)
is optional.
/N
Causes CHOICE not to display
the prompt. The text before the promt is still displayed, however. If you
specify the /N switch, the specified keys are still valid.
/S
Causes CHOICE to be case sensitive.
If the /S switch is not specified, CHOICE will accept either upper and
lower case for any of the keys that the user specifies.
/T[:]c,nn
Causes CHOICE to pause for
a specified number of seconds before defaulting to a specified key. The
values for the /T switch are as follows :
c
Specifies the character
to default to after nn seconds. The character must be in the set of choices
specified in the /C switch.
nn
Specifies the number of secunds
to pause. Acceptable values are from 0 to 99. If 0 is specified, there
will be no pause before defaulting.
CHOICE - Note
ERRORLEVEL-parameters
The first key you assign returns
a value of 1, the second a value of 2, the third a value of 3, and so on.
If the user presses a key that is not among the keys you assigned, CHOICE
sounds a waring beep (that is, it sends a BEL or 07h, character to the
console).
If CHOICE detects an error
condition, it returns an ERRORLEVEL value of 255. If the user presses CTRL+BREAK
or CTRL+C, CHOICE returns an ERRORLEVEL value of 0.
When you use ERRORLEVEL parameters
in a batch program, list them in decreasing order.
CHOICE - Examples
What the user sees when you
use CHOICE in a batch file.
If you use the following syntax
in a batch file,
choice /c:ync
the user sees the following
when CHOICE is started :
[Y,N,C] ?
If you add text to the syntax,
choice /c:ync Yes, No, Continune
the user sees the following
when CHOICE is started :
Yes, No, Continune [Y,N,C]
?
What the user sees if you leave
out a prompt.
If, as in the following example,
you use the /N switch to leave out the prompt in a batch program,
choice /n Yes, No, Continune
?
the user sees only the text
you specified when CHOICE is started :
Yes, No or Continune ?
What the user sees if you use
the /T switch.
If you use the following syntax
in a batch program,
choice /c:ync /t:n,5
the user sees the following
when CHOICE is started :
[Y,N,C] ?
If after 5 seconds, the user
hasn't pressed a key, CHOICE chooses N and returns to an ERRORLEVEL value
of 2. If the user presses a key before 5 seconds, CHOICE returns to the
value corresponding to the user's choice.
To have the option of defragmenting
drive C when you start your computer, you could add the following lines
to your AUTOEXEC.BAT file :
choice Defrag drive /ty,5
if errorlevel 2 goto SkipDefrag
defrag c:
:SkipDefrag
If you press N within 5 seconds,
DEFRAG will not run and CHOICE returns an ERRORLEVEL value of 2. If you
do not press N within 5 seconds, or if you choose Y, DEFRAG is run on drive
C.
Using CHOICE in a batch
program.
The following batch program
demonstrates using the CHOICE option to select one of three programs :
MS-DOS Editor, Microsoft Anti-Virus, or Microsoft Backup.
Notice that the IF ERRORLEVEL
statements are listed in decreasing oreder. MS-DOS will consider the IF
statement true if the ERRORLEVEL parameter returned by CHOICE is greater
than or equal to the parameter specified in the IF command.
@echo off
cls
echo.
echo A Microsoft Editor
echo B Micrsoft Anti-Virus
echo C Microsoft Backup
choice /c:abc Choose An Option
if errorlevel 3 goto MSBackup
if errorlevel 2 goto Msav
if errorlevel 1 goto Edit
:Edit
edit
goto End
:Msav
msav
goto End
:Msbackup
msbackup
goto End
:End
ECHO
Displays or hides a the text
in batch programs when the program is running. Also indicates wheter the
command-echoing feature is on or off.
When you run a batch program,
MS-DOS typically displays (echoes) the batch program's commands on the
screen. You can turn this feature on or off by using the ECHO command.
Syntax
ECHO [ON|OFF]
To use the echo command to
display a message, use the following syntax :
echo [message]
Parameters
ON|OFF
Specifies wheter to turn the
command-echoing feature on or off. To display the current ECHO setting,
use the ECHO cpmmand without a parameter.
message
Specifies text you want MS-DOS
to display on the screen.
Related Command
For information about suspending
the execution of a batch program, see the <PAUSE> command.
ECHO - Notes
Using a message with
the ECHO-command
The ECHO message command is
useful when ECHO is off. To display a message that is several lines long
without displaying other commands, you can include several ECHO message
commands after the ECHO OFF command in your batch program.
Hidding the command prompt.
If you use the ECHO OFF command
on the command line, the command prompt does not appear on your screen.
To redisplay the command prompt, type ECHO ON.
Preventing MS-DOS from
echoing a line.
You can insert a sign (@) in
front of a command in a batch program to prevent MS-DOS from echoing that
line.
Echoing a blank line.
To echo a blank line on the
screen, you can type ECHO and then a period (ECHO.). There must be no intervening
space.
Displaying pipes and
redirection characters
You cannot display a pipe (|)
or redirection character (< or >) by using the ECHO command.
ECHO - EXAMPLES
The following example shows
a batch program that includes a three-line message preceded and followed
by a blank line :
echo off
echo.
echo This batch program
echo formats and checks
echo new disks
echo.
If you want to turn ECHO off
and do not want to echo the ECHO command
itself, include an sign (@)
before the command, as follows :
@echo off
You can use the IF and ECHO
commands on the same command line, as follows :
if exists *.rpt echo The report
has arrived.
FOR
Runs a specified command for each line in a set of
files. You can use this command in batch programs or at the command prompt.
Syntax
To use FOR in a batch program, use the following
syntax :
FOR %%variable IN (set) DO command [command-parameters]
To use FOR from the command prompt, use the following
syntax :
FOR %variable IN (set) DO command [command-parameters]
Parameters
%%variable or %variable
Represents a replacable variable. The FOR command
replaces %%variable (or %% variable) with each text string in the specified
set until the command (specified in the command parameter) processes all
the files. Use %%variable to carry out the FOR command within a batch program.
Use %variable to carry out FOR command from the command prompt.
(set)
Specifies one or more files or text strings that
you want to process with the specified command. The parantheses are required.
command
Specifies the command that you want to carry out
on each file included in the specified set.
command-parameters
Specifies any parameters or switches that you want
to use with the specified command (if the specified command uses any parameters
or switches).
FOR - Notes
Using the IN and DO keywords
IN and DO are not parameters,
but they are required in the FOR command. If you omit either of these keywords,
MD-DOS displays an error message.
Using the replaceable
variable
To avoid confusion with the
batch parameters %0 trough %9, you can use any character for variable except
the numerals 0 through 9. For simple batch programs, a single character
such as %%F may be all that is necessary.
You can use multiple values
for variable in complex batch programs to distinguish different replaceable
variables. However, you cannot nest (add) multiple FOR commands on the
same line.
Specifying a group of
files
The set of parameter can represent
a single group of files or several groups of files. You can use wildcards
(* and ?) to specify a files set. The following are valid sets :
(*.doc)
(*.doc *.txt *.me)
(jan*.doc jan*.rpt feb*.doc
feb*.rpt)
(ar??1991.* ap??1991.*)
When you use the FOR command,
the first value in set replaces %%variable (or %variable) and MS-DOS carries
out the specified command in order to process this value ; this continues
until MS-DOS has processed all the files (or groups of files) that correspond
to the value (or values) in set.
FOR - Examples
Suppose you want to use the
TYPE command to display the contests of all the files in the current directory
that have the extension .DOC or .TXT. To do this and to use the replaceable
variable %F, type the following command at the command prompt :
for %f in (*.doc *.txt) do
type %f
In this example, each file
that has the .DOC or .TXT extension in the current directory is substituted
for the %F variable until the contents of every file are displayed. To
use this command in a batch file, you would replace every occurence of
%F with %%F. Otherwise, MS-DOS ignores the variable and displays an error
message.
MS-DOS supports command switches,
pipes, and redirection that you may want to use with the specified command.
For example, to redirect the output of the previous example to PRN (the
default printer port), you would type the following command :
for %f in (*.doc *.txt) do
type %f > prn:
GOTO
Directs MS-DOS to a line in
a batch program that is marked by a label you specify. You can use this
command only in batch programs.
The GOTO commanddircts MS-DOS
within a batch program to a line identified by a label. When MS-DOS finds
the label, it processes the commands beginning on the next line
Syntax
GOTO label
Parameters
label
Specifies the line in a batch
program to wich MS-DOS should go.
GOTO - Notes
Valid values for label
The label parameter cannot include separators such
as spaces, semicolons or equal signs.
GOTO uses the eight first characters of each
label
The GOTO command uses only the first eight characters
of a label. Therefore, the label "hithere01" and "hithere02" are both equivalent
to "hithere0".
Matching the label parameter with the label
in the batch program
The label value you specify on the GOTO command line
must match a label in the batch program. The label within the batch program