Using Batch Files, Part 3: The IF Command

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

Customizing What Appears on the Right Side of the Start Menu

You're probably used to seeing your Start menu appear a certain way. But is it the most effective display for how you ...

Discover More

Creating a Simple Batch File

One of the powerful capabilities built into Windows is the ability to create and use batch files to perform a wide ...

Discover More

Using Powercfg to Change the Name of a Power Scheme

The Powercfg utility allows you to control how power is used on your system. This tip shows you how to use the ...

Discover More
More WindowsTips

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

Passing Parameters to a PowerShell Script

Like the older batch-file processor, PowerShell can accept parameters. This allows for flexibility in your script. This ...

Discover More

Running a Batch File at a Scheduled Time

Once you become comfortable with batch files, chances are that you'll want to use them to perform various system ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

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}] 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 5 + 9?

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


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.