|
IF
Performs boolean operations in batch files. Performs
conditional processing in batch programs.
Syntax
{help [command]|[command]/?}
File syntax
IF [NOT] EXIST filename command
IF [NOT] EXIST filename (command) ELSE (command)
String syntax
IF [/I] [NOT] item1==item2 command
IF [/I] item1 compare-op item2 command
IF [/I] item1 compare-op item2 (command) ELSE (command)
Error Check Syntax
IF [NOT] DEFINED variable command
IF [NOT] ERRORLEVEL number command
IF CMDEXTVERSION number command
key
item : May be a text string or an environment
variable a variable may be modified using
either.
Substring syntax or Search syntax.
command : The command to perform .
NOT : perform the command if the condition is
false.
== : perform the command if the two strings are
equal.
/I : Do a case Insensitive string comparison.
compare-op : may be one of
EQU : equal
NEQ : not equal
LSS : less than <
LEQ : less than or equal <=
GTR : greater than >
GEQ : greater than or equal >=
This 3 digit syntax is necessary because
the > and < are recognised as redirection
symbols.
When a program stops, it returns an exit code. You can use
exit codes as conditions by using the errorlevel parameter.
If the condition specified in an IF command is true, the
command that follows the condition is carried out. If the
condition is false, the command in the IF clause is ignored,
and executes any command in the ELSE clause, if one has been
specified.
You must use the ELSE clause on the same line as the command
after the IF.
You can improve the readability of a batch script by writing
a complex IF...ELSE command over several lines using
brackets.
To deliberately raise an ERRORLEVEL in a batch script use
the EXIT /B command.
Examples
The following example tests for the existence of a
directory:
if exist c:\mydir\nul goto proce
Cmd Commands
|