Using Batch Files, Part 3: The IF Command

Written by Barry Dysert (last updated September 3, 2018)

This tip continues the series about using batch files. It is suggested that you (re)visit the previous tips so that we can pick up where we left off. Thus far we've covered the following commands: DIR, REM, ECHO, FIND, TIME, and TIMEOUT. We've also used the following operators: output to file (>), remark (::), no echo (@), pipe (|), and concatenate (&). Finally, we know that we use parameters passed to a batch file via the combinations %1, %2, %3, etc.

This tip will focus on commands that are related to performing conditional processing in a batch file. These include IF, CHOICE, GOTO, and the special syntax of ":label".

I have a very handy batch file called Do.bat. It is little more than a "wrapper" that runs programs regardless of where these programs reside. So, for example, if I want to run the program called Prog1.exe I would simply type

C:\Bat> DO Prog1

If I wanted to run Prog2.exe I would type

C:\Bat> DO Prog2

The beauty of Do.bat is that when I created it, I entered the locations of all the programs that I want it to work with. So I don't have to remember, for example, that Prog1.exe lives at "C:\Programs\My Programs\Prog1.exe" and that Prog2.exe lives at "D:\Utilities\Barry\Prog2.exe". Plus, I never have to type those long path names again.

My Do.bat file looks like this:

@ECHO OFF
GOTO %1
:Prog1
"C:\Programs\My Programs\Prog1.exe"
GOTO Done

:Prog2
"D:\Utilities\Barry\Prog2.exe"
GOTO Done
:Done

The first line of the file ensures that the other lines won't be displayed. This is followed by the GOTO line, which takes as a parameter the name of the label that control is to be transferred to. And it so happens that this label is also the parameter that was passed to Do.bat to begin with. So when you invoke Do.bat and pass "Prog1" as the parameter, the .bat file does a GOTO to a label named ":Prog1" (these are not case sensitive). Execution follows sequentially from top to bottom, so the next command to be executed is the one that invokes Prog1.exe. After this program is finished, the .bat file continues by executing the "GOTO Done" command. The ":Done" label is the last line of the file, so when it reaches this point, the .bat file exits. (There is an EXIT command, but if we were to execute that, it would terminate not only the .bat file but also the entire CMD session, which is probably not what you want to do.)

You could alternatively use the CHOICE command and perhaps create a simple menu from which to choose what program to run. In this case I might make a file called Choice.bat which would look like this:

@ECHO OFF
ECHO Select 1 to run Prog1
ECHO Select 2 to run Prog2
ECHO Select Q to quit
CHOICE /C 12Q /T 10 /D Q
GOTO %ERRORLEVEL%
:3
ECHO Quitting
GOTO Done
:2
"D:\Utilities\Barry\Prog2.exe"
GOTO Done
:1
"C:\Programs\My Programs\Prog1.exe"
GOTO Done
:Done

The CHOICE command provides the user with a prompt to enter a 1, 2, or Q (by default, it is not case sensitive). Any other character is ignored. The /T switch says to timeout in 10 seconds, at which time the procedure will behave as if the Q option was selected (/D Q says the Default option is Q). After the CHOICE command executes, the variable %ERRORLEVEL% is set to ordinal value of the option selected. So if the user selected 1, %ERRORLEVEL% is set to 1; if the user selected 2, %ERRORLEVEL% is set to 2; and if the user selected Q, %ERRORLEVEL% is set to 3. We then GOTO the label indicated by %ERRORLEVEL% and perform the requested action.

I could write an entire tip on the IF command, but the basics are that you do something like:

IF arg1 operator arg2 command

where 'arg1' and 'arg2' are arguments being tested, 'operator' is one of EQU, NEQ, LSS, LEQ, GTR, GEQ (i.e., EQUal, Not Equal, LeSS than, etc.), and 'command' is a command-line command.

A simple batch file to echo P1 if there is one might look like this:

@ECHO OFF
IF [%1] EQU [] ECHO No Parameter passed
IF [%1] NEQ [] ECHO Parameter P1 is '%1'
GOTO:eof

(I got in the habit of using [] around my arguments to avoid certain complications.)

There are other flavors of IF too. For example, you can test an error level, test if a file exists, include an ELSE statement, etc.

At the command line, do an IF/? and you'll get a good idea of what all you can do.

 This tip (13185) applies to Windows 7, 8, and 10.

Author Bio

Barry Dysert

Barry has been a computer professional for over 35 years, working in different positions such as technical team leader, project manager, and software developer. He is currently a software engineer with an emphasis on developing custom applications under Microsoft Windows. When not working with Windows or writing Tips, Barry is an amateur writer. His first non-fiction book is titled "A Chronological Commentary of Revelation." ...

MORE FROM BARRY

Understanding Windows Firewall

There are a lot of malicious users on the Internet who are trying to break into other people's systems. One way to ...

Discover More

Repairing Your System Using a System Repair Disk

If your system gets into a state where it cannot be started normally, this tip presents you with options of how you can ...

Discover More

Adjusting Interaction Settings in the Ease of Access Center

Windows 10's Ease of Access Center allows you to configure your computer to be easier to use if you have certain ...

Discover More
More WindowsTips

Using Batch Files, Part 2

This tip is part of a series that talks about Windows batch files. It introduces a few more commands and special ...

Discover More

Using the SHIFT Statement

Sometimes your batch file needs to accept an unknown number of parameters. This is easy to deal with if you know about ...

Discover More

Using the FOR Statement

In another tip we were introduced to the various FOR loops that exist in Windows. The actual use of these loops was left ...

Discover More
Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 2?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


Newest Tips