Windows 7 / Getting Started

Creating Batch Files

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.

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. (See the "Using the Goto Command" section of this tutorial for details.)

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. 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. See the "Using Variable Substitution" section of the tutorial for details on using this technique.

[Previous] [Contents] [Next]