In my previous articles, “DOS is *not* dead” and “DOS is *still* not dead, we built up your support tech arsenal by delving into the depths of DOS batch file programming and examining the XCOPY32, DISKCOPY, ECHO, PATH and > (redirect) commands. In this installment of the DOS is *not* dead series, we’ll learn about a few more DOS commands that are useful for support techs to have up their sleeves—SET, GOTO, and IF.

DOS is SET in its ways
The SET command allows you to create what are known as variables. These variables are objects that allow us to store information for later use in our batch file. The syntax for the SET command is:
SET testvar=123456

The variable named “testvar” is now set to the value of 123456. To confirm this, we return to our good friend, the ECHO command. Once a variable is set, we need to enclose it with percentage marks (%) so that our batch file knows the difference between it and random text:
ECHO %testvar%

This should display the string 123456 on your screen. If it does not, make sure you have used the same DOS window to run both commands. The variables do not transfer between sessions and are deleted when you close the session in which they were created. Another method of checking whether a variable has been properly created is to simply run the SET command by itself. This will list all of the variables that exist on your computer at the time, including all the system variables that are created by your OS.

GOTO: Traveling around the batch file
The GOTO command is another useful tool. It is most often used with the IF command (see below), and it allows a batch programmer to skip forward or backward in a batch file. This is accomplished through the use of labels. To create a label, simply type a : (colon) followed by a single word. Then use the GOTO command to reference that label (without the leading colon). Here is a basic batch file showing GOTO in action (with a little help from the ECHO command):
ECHO Hello
ECHO World

When this batch file is run, the word ”Hello” is printed on the screen, then the batch file hits the GOTO command and skips over the ”ECHO World” command and jumps down to the :FINISHED label, at which point it hits the EXIT command, thereby ending the program. As I said earlier, GOTO doesn’t really shine until combined with the IF command.

IF: Who knew two letters could do so much?
The IF command is possibly the most useful command when creating DOS batch files. If you have done any other programming, you have almost certainly heard of IF-THEN-ELSE statements. For those of you who aren’t familiar with this concept, it can be described this way: IF X condition is met, THEN do Y, ELSE do Z. Here’s another example: IF the cost equals $50 or less, THEN buy it, ELSE keep looking for a lower price. DOS, however, doesn’t use the words THEN or ELSE when handling IF strings. In fact, without the existence of the GOTO statement, the ELSE part of this equation wouldn’t be possible. Here’s how DOS handles the IF statement:

To handle a simple IF/THEN-type statement, we need to use IF combined with our conditions and our desired result:
       @ECHO off
       SET testvar=2
       IF %testvar%==2 ECHO Correct!!!

This bit of code will display the word “Correct!!!” on your screen, whereas the following batch file will display nothing, as the conditions tested by the IF command do not match:
       @ECHO off
       SET testvar=3
       IF %testvar%==2 ECHO Correct!!!

An important item to mention here is the use of the double equal signs in the IF statement. This is not a typo. It is a requirement of the command itself when checking to see if two strings are equal. Now, what if we wanted to check if the two strings were not equal? All we need to do is add the word NOT after IF, like so:
       @ECHO off
       SET testvar=3
       IF NOT %testvar%==2 ECHO Correct!!!

This code will display the word “Correct!!!” this time because the strings did not match—which was exactly what we wanted to determine.

Using IF to determine if a file exists
Often, we want to determine if a certain file exists. To do this with IF, simply add the word EXIST after the IF command. Here’s an example:
       @ECHO off
       IF EXIST C:\autoexec.bat ECHO Correct!!!

On just about every system, this should return the “Correct!!!” display. If it does not, then the C:\autoexec.bat file does not exist on your system. But what if you want to check if a directory exists, regardless of whether it contains files? You have to check for the existence of the “file” NUL, which exists in every directory:
       @ECHO off
       IF EXIST C:\WINDOWS\NUL ECHO Correct!!!

If you are using a standard Windows install, this will once again display “Correct!!!.” If it does not, then you may have a different Windows directory than the default.

Now that we have covered the basics of the IF statement, it is time to make things a bit more interesting. The true power of IF comes into play when you can examine a statement for its validity and then provide a response regardless of whether it is true or false (something that the ELSE statement does in other programming languages). Now, I imagine the natural inclination for most people would be to use something like the following:
       @ECHO off
       SET testvar=2
       IF %testvar%==2 ECHO Correct!!!
       ECHO Incorrect!!!

If this were done, once the program has reached the stage where it realizes that the IF statement has proven to be correct, it should stop. However, this is not the case. The program will accept the strings as being the same and display “Correct!!!,” but then it will continue on the next line and display “Incorrect!!!.” This turn of events creates an obviously less than ideal situation. The next step most people would likely attempt when faced with this dilemma would be to add an EXIT command to the end of the IF statement, like so:
       @ECHO off
       SET testvar=2
       IF %testvar%==2 ECHO Correct!!! EXIT
       ECHO Incorrect!!!

A nice idea, but this won’t work, either. All this does is change the display of the first line to “Correct!!! EXIT.” No amount of adding commas, colons or semicolons will make any difference here, either, because the simple truth is that DOS can only process one command after an IF statement.

Now that we know what DOS can’t do with the IF statement, how do we get around it? This is where the GOTO command is truly useful. By combining the IF and GOTO commands, we can come as close to a true IF-THEN-ELSE statement as is possible when creating DOS batch files:
       @ECHO off
       SET testvar=4
       IF %testvar%==2 GOTO 2TRUE
       IF %testvar%==3 GOTO 3TRUE
       ECHO The test variable was not set to 2 or 3.
       GOTO END
       ECHO The test variable was set to 2
       GOTO END
       ECHO The test variable was set to 3.
       GOTO END

This will display “The test variable was not set to 2 or 3.” Change the second line of the program to SET testvar=2, and “The test variable was set to 2” will be displayed. Note that I could have skipped the use of the GOTO END command at the end of the :3TRUE section. The reason I did not, and the reason I think you should avoid this practice as well, is that I may want to add more commands to my program at a later date. If I do want to add more commands, haven’t included GOTO END previously, and don’t remember to add the GOTO END command at that later date, my program is headed for trouble. You are definitely much better off increasing the size of your file by eight characters now to be sure your program will work in the future.
I’ve received a few e-mails regarding my previous articles asking me why these commands aren’t working for some of you. Almost invariably, they start off with something like “I went into the NT Command prompt…” or “I am running Windows NT 4.0 and…” While most DOS and NT command line commands are similar, they can also be very different. Unfortunately, nothing can be done to remedy this, other than using a Windows 9x machine.
I hope you’ve enjoyed my series on how DOS can still be a powerful tool for the frontline technician and that you’ve learned a few tricks that can make your job easier. The SET, GOTO, and IF commands can perform a variety of functions, and I’ve only touched on a few in this article. Stay tuned for more articles on why DOS lives on.
To comment on this tip or to share your own favorite DOS batch file technique, please post a comment or write to Jason.