Name

if

Synopsis

Execute a command if certain conditions are met.

Syntax

if [not] string1==string2 
                           command [arguments]

if [/i] string1 compare-op string2 
                           command [arguments]

if [not] exist filename 
                           command [arguments]

if [not] errorlevel n 
                           command [arguments]

Description

Conditional branching lets your batch file test to see whether a condition is true, and if it is, instructs the batch file to execute a command or continue execution at another location in the batch file (via the goto command). The following options can be used with the if command:

command [ arguments ]

The command to execute or the program filename to run. This can be anything you’d normally type at a command prompt; arguments are the options, if any, to pass to the command.

not

Specifies that command should be carried out only if the condition is false; not valid with compare-op.

string1==string2

Specifies a true condition if the specified text strings match. String1 and string2 must be enclosed in quotation marks or parenthesis.

string1 compare-op string2

Performs a more flexible comparison than string1 == string2, shown above. The compare-op term can be one of the following:

EQU

Equal

NEQ

Not equal

LSS

Less than

LEQ

Less than or equal

GTR

Greater than

GEQ

Greater than or equal

/i

Specifies a case-insensitive comparison; used only with compare-op.

exist filename

Specifies a true condition if the specified file exists.

errorlevel n

Specifies a true condition if the previous command or program returned an exit code ...

Get Windows XP in a Nutshell, Second Edition 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.