Batch files are a type of simple programming that can
help you store a series of commands that you want to execute more than
once. The batch file normally appears within a file with a BAT
extension. In most cases, you won't need to perform any programming to
create a batch file; simply create a file that contains the commands you
want to execute one after the other.
NOTE
Windows doesn't support the Break command found in many older batch files. The original purpose of the Break command was to provide control over the Ctrl+Break key. Setting Break ON
would let someone press Ctrl+Break to stop execution of a batch file.
Windows ignores this command line switch. In addition, Server Core
changes support for some batch commands from previous versions of
Windows and adds new commands. Consequently, batch files that worked
fine in Windows XP may suddenly stop working in Server Core. (If you've
already updated your batch files to work in Vista, they'll also work
fine in Server Core.)
Even with the limitations of a
batch file, however, you might be surprised at the number of ways that
people use them. For example, if you find that you're constantly
forgetting how to perform tasks at the command line, create a menu
system with your favorite commands. That way, you only have to look up
the command information one time. The following sections describe the
programming features of batch files and provide you with some sample
batch files.
1. Using the Call Command
You use the Call
command to call another location with the current batch file or to
start another batch file. When you want to call another location in the
same batch file, you use the label formatting shown here:
Call :MyLabel
Calling another batch
file is similar. You provide the drive, path, and filename of the batch
file. In addition, you can provide command line arguments for the
external batch file as shown here:
Call C:\MyBatchFiles\MyBatch.BAT CommandArg1
A call is
different from going to another location. When a call completes, the
batch file returns to the calling location and executes the next
instruction. In contrast, when you use the Goto command, the batch file actually transfers control to the new location. The return feature of the Call
command lets you create an advanced programming construct called
recursion. Recursion occurs when a batch file calls itself. You must
provide an exit strategy, however, or the batch file will enter an
endless loop.
The easiest way to see the effect of the Call command is to create two batch files named Batch1.BAT and Batch2.BAT. Here's the content for Batch1.BAT.
@ECHO OFF
Call Batch2.BAT
Call Batch2.BAT Passed %1 %PATH%
ECHO In Batch 1
GOTO :EOF
ECHO Goodbye
Here's the content for Batch2.BAT.
ECHO In Batch 2, Goodbye
IF NOT [%1]==[] ECHO String: %1
IF NOT [%2]==[] ECHO Batch 1 Input: %2
IF NOT [%3]==[] ECHO Environment Variable: %3
Looking at the Batch1.BAT
content, the example begins by turning off echo. You'll normally add
this code to your batch files so the user doesn't see a lot of confusing
text that has nothing to do with the current task. Preceding the ECHO command with the @ symbol tells the system not to display the ECHO command either. The first call to Batch2.BAT doesn't pass any information, so Batch2.BAT only displays the message, "In Batch 2, Goodbye." The second call to Batch2.BAT
passes the three kinds of information you can include with a batch
file: a string, a local variable (argument), and a global variable. The
code then proceeds to display "In Batch 1," and then it exits. The GOTO :EOF
statement is special; it tells the batch file to end now. You don't
have to define a label, in this case, because EOF is built into the
command process.
The Batch2.BAT file always echoes "In Batch 2, Goodbye." In this case, the IF
statements verify that the caller has passed information to the batch
file. When the caller doesn't pass the required variables, then the
batch file doesn't display any information for that input. The [%1]==[] construct is one way to check for an empty input. Figure 1
shows the output from this application. Notice the sequence of events.
The first batch file calls the second batch file. When the second batch
file is finished, execution continues with the next statement in the
first batch file.
Windows provides
enhanced methods of working with variables in batch files. These
enhanced expansions help you pass specific variable information to a
callee from your batch files.
2. Using the Choice Command
The Choice
command lets you add interactive processing to batch files. Whether you
use this option depends on the kind of automation you want to add to
your processing tasks. Most of the automation you create for
optimization tasks won't require any kind of interactivity because you
already know how you want the task performed based on experience you
obtained performing the task manually. However, sometimes you do need to
add some interactivity. For example, you might run the command one way
on Friday and a different way the rest of the week. The Choice
command can also help you add safeguards that ensure the user
understands the ramifications of performing a certain task before they
actually do it. Vista and Server Core change the Choice command significantly, breaking many batch files. The Vista and Server Core form of the Choice
command differs not in arguments, but in how you combine those
arguments at the command line. Here's the command line for Vista and
Server Core:
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
The changes make the command clearer, but they break existing batch files in a way that you can't easily fix. The new /CS command line switch lets you make choices case sensitive, so you can have 26 additional menu choices. However, notice that /T no longer takes both a default option and a timeout value. The new form requires that you provide a choice using the /D command line switch instead. You must also provide the /M
command line switch to specify optional text. The following sample code
performs the same task, but the first form works in Windows XP and
earlier, while the second form works in Vista and Server Core.
Old Choice Command Form
CHOICE /C:N /N /T:N,15
Vista and Server Core Choice Command Form
CHOICE /C N /N /T 15 /D N
NOTE
Vista and Server Core provide alternatives for the Choice
command. The TimeOut utility provides a specific timeout value without
requiring the user to make a choice. The WaitFor utility lets you use signaling
between systems or applications on the same system. One application
sends a signal and another reacts when it receives the signal.
When you use Choice by itself, it displays a simple [Y,N] prompt that doesn't accomplish much unless you also provide an Echo command to describe what the user should say yes or no to. Normally, you'll combine the Choice command with one or more arguments. Listing 1 shows a simple example of the Choice command at work.
Example 1. Using the Choice Command
Echo Off
REM Keep repeating until the user enters E.
:Repeat
REM Display the choices.
Choice /C DCE /N /T 15 /D E /M "Choose an option (D)isplay, (C)opy, $$
or (E)nd."
REM Act on the user choice.
If ErrorLevel 3 Goto End
If ErrorLevel 2 Goto Copy
If ErrorLevel 1 Goto Display
REM Copy the file.
:Copy
Echo You chose to copy the file.
Goto Repeat
REM Display the file.
:Display
Echo You chose to display the file.
Goto Repeat
REM End the batch processing.
:End
Echo Goodbye
Echo On
|
The code begins by
creating a repeat label so the batch file continues working until the
user specifically stops it. Next, the code uses the Choice command to display the choices to the user. The /C switch tells Choice
that the valid options are D, C, or E instead of the default Y or N.
Because the text specifically defines the characters that the batch file
expects, the batch file uses the /N switch to suppress displaying the valid key choices on the command line. The /T command line switch tells Choice to automatically choose E after 10 seconds. The /D command line switch provides the default choice of E. Finally, the /M command line switch provides the message displayed to the user.
Although this batch
file doesn't actually do anything with a file, it shows how you'd set up
the batch file to process the user choice. Notice that the batch file
uses the ErrorLevel clause of the If
statement to detect the user choice. The ErrorLevel clause detects every
choice lower than the user selection, so you must place the values in
reverse order, as shown. In addition, you must specifically set the
batch file to go to another location because it will process all other
statements after the current error level.
The processing code
simply displays a string telling you what choice the user made.
Normally, you'd add tasks that the batch file should perform based on
the user's selection. Notice that the copy and display selections tell
the batch file to go back to the Repeat label. This is the most common
technique for creating a menu loop in a batch file. The batch file ends
by telling the user goodbye and turning echo back on.