certainly looks a lot nicer, don’t you think?
:START
...
... (Batch file commands)
...
As you can see, GOTO just leaps over the comments to end up at the :START label.
Windows XP doesn’t even know that the comments exist.
IF: Handling Batch File Conditions
We make decisions all the time. Some are complex and require intricate levels of logic to
answer (Should I get married? Should I start a chinchilla farm?). Others are simpler and
depend only on existing conditions (the proverbial fork in the road):
If it’s raining, I’ll stay home and work. Otherwise, I’ll go to the beach.
If this milk smells okay, I’ll drink some. Otherwise, I’ll throw it out.
No batch file (indeed, no software program yet developed) is sophisticated enough to
tackle life’s complex questions, but the simpler condition-based decisions are no problem.
Here are a few examples of what a batch file might have to decide:
If the
%2 parameter equals /Q, jump to the QuickFormat section. Otherwise, do a
regular format.
If the user forgets to enter a parameter, cancel the program. Otherwise, continue
processing the batch file.
If the file that the user wants to move already exists in the new folder, display a
warning. Otherwise, proceed with the move.
If the last command failed, display an error message and cancel the program.
Otherwise, continue.
For these types of decisions, you need to use the
IF batch command. IF has the following
general form:
IF condition command
condition This is a test that evaluates to a yes or no answer (“Did the user
forget a parameter?”).
command This is what is executed if the condition produces a positive
response (“Cancel the batch file”).
The next few sections discuss the various ways you can use IF in your batch files.
APPENDIX C Automating Windows XP with Batch Files740
Testing Parameters with IF
One of the most common uses of the IF command is to check the parameters that the
user entered and proceed accordingly. For example, the simple batch file from the previ-
ous section that used
GOTO can be rewritten with IF as follows:
@ECHO OFF
CLS
IF “%1”==”A” ECHO This part of the batch file runs if A is the parameter.
IF “%1”==”B” ECHO This part of the batch file runs if B is the parameter.
The condition part of an IF statement is a bit tricky. Let’s look at the first one: “%1”==”A”.
Remember that the condition is always a question with a yes or no answer. In this case,
the question boils down to the following:
Is the first parameter (%1) equal to A?
The double equal sign (==) looks weird, but that’s just how you compare two strings of
characters in a batch file. If the answer is yes, the command is executed. If the answer is
no, the batch file moves on to the next
IF, which checks to see whether the parameter
is
“B”.
NOTE
Strictly speaking, you don’t need to include the quotation marks (). Using %1==A accomplishes
the same thing. However, I prefer to use them for two reasons: First, it makes it clearer than the
IF condition is comparing strings; second, as you’ll see in the next section, the quotation marks
enable you to check whether the user forgot to enter a parameter at all.
CAUTION
This batch file has a serious flaw that will prevent it from working under certain conditions.
Specifically, if you use the lowercase “a” or “b” as a parameter, nothing happens because, to the
IF command, “a” is different from “A”. The solution is to add extra IF commands to handle this
situation:
IF “%1”==”a” ECHO This part of the batch file runs if a is the parameter
Checking for Missing Parameters
Proper batch file techniques require you to not only check to see what a parameter is,
but also whether one exists at all. This can be vital because a missing parameter can
cause a batch file to crash and burn. For example, earlier I showed you a batch file called
DONTCOPY.BAT designed to copy all files in the current folder to a new destination (given
by the second parameter) except those you specified (given by the first parameter). Here’s
the listing to refresh your memory:
IF: Handling Batch File Conditions 741
C

Get Microsoft Windows XP Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.