Classic Computer Magazine Archive COMPUTE! ISSUE 137 / JANUARY 1992 / PAGE S20

How to write DOS batch files. (MS-DOS Featuring DOS 5.0)
by Tony Roberts

A batch file is simply a small text file that can carry out a series of DOS commands in sequence. Batch files can save time, eliminate repetitive keystrokes, minimize typographical errors, and reduce frustration.

Some computer users avoid batch files because they think there's something mysterious or difficult about them. But batch files are just simple conveniences much like the push buttons on your car radio. Those buttons simplify the process of tuning in your favorite station. On a computer, batch files tune in your favorite software programs.

Although batch files can become complex, a majority of computer users can benefit from a few simple batch files. For example, consider how many times a wekk you run your favorite program.

In the case of a word processing program, you probably use the CD command to change to the word processing directory and then run the command to start the word processor. Later, when you exit the program, you issue the CD command again, this time to return to the root directory.

All of this activity can be combined into a simple batch file.

CD\WRITING EDITOR CD\

Save these lines, substituting the appropriate subdirectory and program names, in a file called WP.BAT. After that, getting your word processing program started is as easy as typing WP and as fast as pressing a button on the radio.

Batch-o-matic

Creating batch files is relatively easy. Simply learn a few batch commands and combine them with the DOS commands you already know. Here's a brief glossary of elementary batch commands.

REM. The REM command is short for remark and is used to annotate your programs. Any line in your program that begins with REM is ignored by the command processor. (While the command processor doesn't act on REM lines, they will be output to the screen if you haven't turned the ECHO off.) It's a good practice to include a REM at the beginning of every program so that you'll have a record of when you created it and what its purpose is:

REM WP.BAT, written 9/3/91, starts the

word processing program.

ECHO. You'll often see the command @ECHO OFF as the first line of a batch program. This command instructs the system not to display the batch commands as they're being executed. This produces a more elegant-display once the program is functioning properly, but you may want to omit this line while writing and debugging a batch program so you can see what's going on.

The ECHO command also is used to display information or instructions for the users of the batch file:

ECHO The word processor is about to start. Press

Ctrl-Break to quit.

PAUSE. This command gives users of your batch files time to read instructions or change disks before the program continues. This command prompts the user to press any key to continue. Once a key is pressed, the program continues unless the user presses Ctrl-Break, which ends the batch process.

Adding these commands to your word processor's batch file will produce something like this.

@ECHO OFF

REM WP.BAT, written 9/3/91, starts the

word processing program.

ECHO The word processor is about to start. Press

Ctrl-Break to quit.

PAUSE

CD \WRITING

EDITOR

CD \

Any action or sequence of steps that you repeat is a candidate for a batch file. Any command that frequently sends you to the manual can be incorporated into a batch file that remembers the details for you. I could never remember the command to format a low-density disk in a high-density drive. Finally, I incorporated that command into a batch file I call LFORMAT.BAT, and the problem is forever solved.

Musical Chairs

Once you get the hang of batch files, you'll want to start using replaceable parameters. Don't be put off by that eight-syllable mouthful. You use replaceable parameters every time you write a check.

When you buy groceries, you could go to the bank, fill out a withdrawal slip, withdraw cash, take it to the store, and pay your purchase. Or you could use a check--sort of a financial batch program. To work in many different situations, checks use replaceable parameters for information such as the date, payee, and amount. Each time you write a check, you fill in the blanks with the appropriate information.

Replaceable parameters in batch files work in much the same way. They allow you to insert specific information at the time the batch file is run. Many word processing programs allow you to specify a filename when you issue the startup command for that program. On my system, the command EDITOR TODOLIST.TXT starts my word processing program and calls my to-do list to the screen.

Replaceable parameters are designated in batch files by percent signs followed by a number from 0-9. When the batch file is run, the contents of the command line are substituted for the replaceable parameters. The name of the batch file itself is substituted for %0, the first word that follows is substituted for %1, and so on.

In the WP.BAT program, add the replaceable parameter %1 to the command that starts the word processor. Then you can run the WP.BAT program by typing WP filename. The filename you specify will be inserted into the spot held by the %1, causing your program and file to be loaded.

You can create batch files that use one or several replaceable parameters to add power and sometimes danger to your programs. Consider this example.

@ECHO OFF

REM TRANS.BAT, written 8/24/91, copies a

file from one disk to another

REM and then deletes the original file. COPY

%1 %2

ERASE %1

If you execute this program by typing TRANS testfile B.; the filename testfile will be substituted for the %1, and the drive designator B: will be substituted for %2. The program will copy the file testfile to drive B: and then delete the original.

It sounds simple, but using the ERASE command without safeguards in a batch file opens the door for trouble. The way this program is written, the original file will be deleted even if the copy is unsuccessful.

If you plan to write batch files that have the power to change and destroy your important data, you'll need to know some other batch commands, specifically IF and GOTO, that can help you avoid disaster.

IF allows you to check conditions, such as whether a certain file exists or whether an operation was completed successfully. GOTO allows you to jump to a different section of your program if conditions warrant.

Also, if your batch program uses replaceable parameters, IF and GOTO can help you write self-documenting batch files. Here's another version of TRANS.BAT that illustrates this:

@ECHO OFF

REM TRANS.BAT, written 8/24/91, copies a

file from one disk to another

REM and then deletes the original file. IF

(%2)==() GOTO HELP

COPY %1 %2

ERASE %1

GOTO END

:HELP

ECHO This programs copies a file to

another drive and deletes the original. ECHO

SYNTAX: TRANS filename destination

:END

The first line following the REM statements tests for the presence of replaceable parameter %2. If no value is found for this parameter, the program jumps to help section, which tells the user what information is expected.

If you use this format for batch programs with replaceable parameters, you'll be able to remind yourself how to use the program simply by executing the program and specifying no parameters.