Command Prompt for Windows




Command IF

Function:

Perform conditional processing in a batch file.

Syntax:

IF [/I][NOT] condition command
or
IF [/I][NOT] condition ( command(s) ) ELSE command
or
IF [/I][NOT] condition { command(s) } ELSE command
/IPerform a case insensitive compare.
conditionIs one of the following :-
( expr )Evaluate the parameter expr. A zero result is false, otherwise the condition is true. Parameter expr follows the same rules as parameter expr for command CALC. The brackets around parameter expr are mandatory and must be specified.
CMDEXTVERSION numberSpecifies a true condition if the extension version is greater then or equal to number.
DEFINED variableSpecifies a true condition if the environment variable exists.
ERRORLEVEL numberSpecifies a true condition if the last program returned an exit code greater than or equal to number.
EXIST filenameSpecifies a true condition if the filename exists.
EXISTCLASS "text"Specifies a true condition if the window class "text" exists.
EXISTWINDOW "text"Specifies a true condition if the window caption "text" exists.
string1==string2Specifies a true condition if string1 and string2 are the same.
string1 comp-op string2comp-op can be one of the following :-
EQUequal
NEQnot equal
LSSless than
LEQless than or equal
GTRgreater than
GEQgreater than or equal

Specifies a true condition when comp-op is satisfied. When string1 and string2 are both integer numbers then both string1 and string2 are converted to integers and a numerical compare is performed.

commandSpecifies the command to carry out if the condition is met. Parameter command can also be another IF command.
NOTCarry out the command only if the condition is false.

Note:

The parameters string1 and string2 do not need to be enclosed in quote characters.

When using the IF ELSE syntax, the brackets or braces around parameter command(s) are mandatory and must be specified. See Command Grouping for more information.

Examples on using IF.

Also see command CALC and Batch Files.