Classic Computer Magazine Archive COMPUTE! ISSUE 152 / MAY 1993 / PAGE 52

Batch file runaround. (use of batch files and MS-DOS) (Column)
by Tony Roberts

Most batch files are merely collections of DOS commands that run straight through from top to bottom. If you're adventurous, however, you can supercharge your batch files by interrupting the linear flow.

CALL (in DOS versions 3.3 and higher) is a batch command that allows you to create nested batch files. Normally, when one batch file is run from within another, full control is handed to the second file. Any commands in the first batch file that appear after activation of the second one are ignored.

The CALL command, however, suspends the first batch file until the second is finished. Then control returns to the first batch file, and processing continues. To achieve this, simply place the keyword CALL before the name of the second batch file.

How can you use this? I like my computer to make my network connections automatically when I boot up, but I'm not interested in cluttering up the AUTOEXEC.BAT file with a dozen network commands. So I include the command CALL GONET in the AUTOEXEC.BAT. When the program runs, AUTOEXEC.BAT temporarily gives control to GONET.BAT. When GONET.BAT is finished, AUTOEXEC.BAT picks up where it left off.

This setup is extremely helpful because I use a couple of TSR programs that don't work unless they're loaded after the network. If I weren't able to use CALL GONET, I'd have to either include all the network commands in the AUTOEXEC.BAT or manually install those TSRS after the network was initialized.

Another way to change batch program flow is to use the GOTO command. GOTO lets your batch programs move in different directions depending on conditions. For example, you could create a single batch file called START BAT that contains startup instructions for several programs.

At the command line, you could type start wp, for example, and the batch file would jump to the WP section and start your word-processing program. Similarly, typing start win could activate Windows, or typing start finance could initialize your financial management software.

This is accomplished with the GOTO command and labels. A label is a batch file line that begins with a colon. Here's how the START BAT program might look. You'll have to replace the REM lines with the appropriate commands for your system. [at]ECHO OFF GOTO %1. GOTO end :wp REM Insert word processor

commands here. GOTO end :win REM Insert Windows startup commands here. GOTO end :finance REM Insert financial manager

commands here. GOTO end :end

When the batch file is executed, the replaceable parameter (the l in the first line) is replaced with the first word you typed after the batch-file name. If you typed start wp, then the %1 would be replaced with wp, and the batch file would jump to the :wp label and begin its execution. Note the line GOTO end, following each section. This prevents the program from running away with itself and executing the commands in every section.

Now that you're getting used to the idea that batch files don't have to run in a straight line, let's create one that runs in circles.

To do this, we'll combine the GOTO command and replaceable parameters used in the previous example with the SHIFT command. Let's say you wanted to display three files on your screen, but you didn't want to type three separate commands. We'll create a batch file, called T.BAT, to do this. To execute this file, you'll type t file1 file2 file3, filling in the names of your own files for file1, file2, and file3.

As you can see, we've entered three replaceable parameters on the command line. The SHIFT command takes all of the replaceable parameters and shuffles them to the left. After SHIFT is executed once, file1 is gone, and file2 is at the head of the list. Each time SHIFT is executed, a new parameter is available to be substituted for l in the batch file. Here's the batch file: [at]ECHO OFF :start IF (%1)==() GOTO end TYPE %1 MORE PAUSE SHIFT GOTO start :end

In this program, the third line makes sure there's a replaceable parameter available. If so, the TYPE command is executed. Then the SHIFT command shuffles the replaceable parameters, and the GOTO command loops the program back to the beginning. As long as a parameter mains on the stack, the looping continues.