IF — Conditionally execute a command

Synopsis

IF [/?] [/I] expression command

Options

/?
Display command syntax information.
/I
Ignore case in string comparisons

Description

If expression evaluates true, the IF command executes command. The result obtained from executing (or attempting to execute) command is returned as the result of the IF command.

If expression evaluates false, no command is executed and the IF command returns as its result the result of the previous command to execute (thereby allowing multiple IF ERRORLEVEL tests to be used in succession).

command must a basic command, and cannot be a pipeline or a sequence of commands. It is not passed through environment variable expansion or redirection processing a second time before being executed. Any redirection, pipeline, or other metacharacters that appear in command, as a result of the environment variable expansion and other parsing of the original command line, are not given any special treatment.

To execute multiple commands in an IF command, use CALL or GOSUB for command in order to execute a separate command script or a subroutine.

Expressions

expression must be one of the following:

NOT expression
This expression evaluates true if and only if the sub-expression expression evaluates false.
EXIST wildcard
This expression evaluates true if and only if one or more files or directories that match the given basic wildcard search specification exist.
EXISTS name
This expression evaluates true if and only if a file or directory with the given name exists.
ISDIR name
This expression evaluates true if and only if a directory of the given name exists.
ISALIAS name
This expression evaluates true if and only if an alias of the given name exists.
ISINTERNAL name
This expression evaluates true if and only if a built in command of the given name exists.
DEFINED name
This expression evaluates true if and only if an environment variable of the given name exists.
ERRORLEVEL number
This expression evaluates true if and only if the result of the last command executed is greater than or equal to the given number.
string1 operator string2
This expression evaluates true if and only if the relational operator evaluates true for the two strings. operator must be one of the following:
==
equality
\=
inequality
GE
greater than or equal
GT
strictly greater than
LE
less than or equal
LT
strictly less than
EQ
equality
NE
inequality

All comparisons are case-sensitive.

Note: If string1 matches any of the keywords NOT, EXIST, ISDIR, or ISALIAS, or if it contains an '=' character or whitespace, it must be quoted. If string2 contains whitespace, it must be quoted.

An easy way to remember the difference between "EXIST" and "EXISTS" is that the former is the plural form of the verb and can apply to more than one file or directory, whereas the latter is the singular form of the verb and applies to just one file or directory. "EXISTS" is useful for testing for the specific existence of files and directories whose names contain basic search wildcard metacharacters.

Notes on redirection

Redirection takes place only once. Because of the way that the command interpreter works, redirection takes place before the IF command is invoked. This means that redirection applies whether expression evaluates true or false.

CALL EXTPROC FOR GOTO GOSUB RETURN CANCEL QUIT EXIT


The 32-bit Command Interpreter is © Copyright Jonathan de Boyne Pollard. "Moral" rights are asserted.