Enterprise Software

Build Your Skills: Make batch files more useful with branching and decision-making techniques

Tips on advanced branching techniques in DOS batch files

Few people would disagree that batch files are a limited type of program. Certainly, batch files will never replace Visual Basic or C++ because of their limitations. However, there are some techniques that you can use to greatly enhance your batch file’s capabilities. Two of my favorite techniques, branching and decision making, are easy to use once you know how to use the IF, THEN, ELSE, GOTO, and CALL commands. Here's how.

Branching
Since batch files are essentially a collection of DOS commands and calls to applications, it’s easy to think of a batch file as a fall-through program. A fall-through program is one that starts at the first line of code and executes the subsequent lines one at a time until it hits the last line. While effective, this process doesn't allow you to build much creativity into your programs. It is possible, however, for batch files to make decisions and to branch based on those decisions if you know the right code to use.

Before I jump into the code you’ll need to know, I want to show you how branching and decision making work. Decisions are made through an IF, THEN, ELSE mechanism. This mechanism by itself allows the batch file to make a decision and to execute a single line of code based on that decision. There are times, though, when you’ll need to execute more than just one line of code after a decision has been made. This is where branching comes in.

Branching is performed via the CALL and GOTO commands. The CALL command forces the batch file to go to a different file. Normally, when a batch file needs to transfer control to another file (such as another batch file), you’d simply enter the name of the batch file as a command. This routine, however, permanently shifts control to the new file. There’s no way for the command interpreter (CMD.EXE) to return to the point where it left off in the original file once the secondary file has completed. The CALL command can fix this issue. Entering the CALL command before the filename of the file that you want to call (example: CALL FILE.BAT) will cause the command interpreter to open the secondary file, execute its contents, and then return to the point that it was called from in the original file when execution completes.

The GOTO command works similarly to the CALL command except that rather than calling another batch file, the GOTO command sends the command interpreter to a designated location within the current batch file.

You can create subroutines within a batch file by labeling blocks of code. You can label a block of code by entering a colon followed by a name just before the block of code. For example, below, you see a sample of pseudo code that has been labeled BRIEN.
:BRIEN
LINE1
LINE2
LINE3


If you wanted to call this block of code from within a batch file, you’d simply issue the command GOTO :BRIEN. To prevent subroutines from being executed unintentionally, you should use the GOTO :EOF command as the last line of your main batch file. This command is the batch file equivalent to an END statement. Assuming you’re using Windows 2000 or a newer operating system, you won’t have to define :EOF. Otherwise you will need to place :EOF at the very end of your batch file. Now, let’s take a look at a pseudo code sample of what all of this might look like together:
LINE1
LINE2
LINE3
GOTO :BRIEN
LINE4
GOTO :EOF

 
:BRIEN
LINE1
LINE2


In the pseudo code sample above, the first three lines of code would be executed, and then the code sample would jump to the :BRIEN subroutine. The code would then execute the two lines of code in :BRIEN and would then jump back to the main section of code, where it would execute line 4 and then terminate the batch file.

Decision making
There are times when you might use CALL or GOTO in a batch file that doesn’t have to make any decisions. An example of this would be a batch file that uses the same block of code over and over. However, the GOTO and CALL commands really shine when used in conjunction with decision making. The IF, THEN, ELSE mechanism used for decision making can normally execute only a single line of code when a decision has been made. That single line of code however, can be a CALL or a GOTO statement, which would then allow an entire block of code to be run after a decision is made.

There are three main types of IF commands used in batch files. These commands include the following:
IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXISTS filename command


You’ll notice that each of these lines of code has the word NOT in a set of brackets. The [NOT] is optional. For example, the IF EXISTS filename command line of code is designed to execute a command if a specific file exists. Using the [NOT] (which doesn’t actually appear in brackets in real code) changes the command in a way that it will execute only if the specific file doesn’t exist. The other two IF statements are designed to check for an error level returned by an application and to compare the value of two strings (often expressed as %1 and %2).

The IF command can also be used in conjunction with an ELSE command. The ELSE command tells the IF command to do one thing if the condition is true, but something else if the condition is false.

For example, the following line of code tests for the existence of a file named BRIEN.TXT. If the file exists, it will be deleted. If the file doesn’t exist, then the batch file will use the ECHO command to display some text saying that the file is missing. The command looks like this:
IF EXISTS brien.txt (erase brien.txt) ELSE ECHO brien.txt is missing

Obviously, this is a pretty simple command. However, in the real world, there may be times that you would need to perform additional tasks in conjunction with the IF, THEN, ELSE mechanisms. To demonstrate how this is done, I’ve created some blocks of pseudo code. The idea is that if the file does exist, a subroutine called :TRUE will be called. If the file doesn’t exist, a subroutine called :FALSE will be called. In each subroutine, I’ve added a few lines of fictitious code just to illustrate the point that you aren’t limited to single commands.
Line 1
Line 2
IF EXISTS brien.txt (GOTO :true) ELSE GOTO :false
Line 3
Line 4
GOTO :EOF


:TRUE
Line 1
Erase brien.txt
Line 2


:False
Line 1
ECHO brien.txt is missing
Line 2

0 comments