Purpose: | Execute a single command if a condition is true |
Format: | IF [/I] condition command |
IF [/I] condition (command1) ELSE (command2)
condition |
|
command |
The command to execute if condition is TRUE. |
command1 |
The command to execute if condition is TRUE. |
command2 |
The command to execute if condition is FALSE. |
See also: Conditional expressions, IFF, @IF.
Usage:
IF is usually used only in aliases and batch files. It is always followed by a condition (see Conditional expressions), and then a command. First condition is evaluated, and if it is TRUE, command is executed. Otherwise, command is ignored.
If the condition is FALSE, IF returns a non-zero result, so it can be evaluated by one of the conditional command operators (II or &&).
Do not use IF with multi-line TCC-RT commands like DO (unless you use the single-line variant of DO).
The IF ... ELSE ... syntax of CMD is also supported:
IF [/I] condition (command1) ELSE (command2)
The commands to be executed must be enclosed in parentheses (as in a command group). If condition is TRUE, command1 is executed, if FALSE, command2 is executed. Note: this syntax is much less powerful than the IFF command, which is recommended.
If the last argument on the line is a single (, it is interpreted as the beginning of a command group. IF will append the following lines (in a batch file) or prompt you for more input (at the command line) until it gets a closing ).
When an IF test fails, the remainder of the command is discarded. Whether TCC-RT continues with the next command on the line, or discards the rest of the line and goes to the next line is dependent upon the Duplicate CMD Bugs configuration option. CMD will discard all remaining commands on the line when an IF test fails, including those after a command separator or pipe character. If you do not want to reproduce CMD.EXE's behavior of an IF affecting all commands on a line, set DuplicateBugs to No in the .INI file. The IF behavior is different when DuplicateBugs is YES in a command group in a batch file. If there are multiple command lines in the command group, a failed IF will only ignore the remainder of the commands on that line. The commands on the subsequent lines in the command group will still be executed.
For example, if Duplicate CMD Bugs is enabled (the default), the following command will display nothing, because the second ECHO command is discarded along with the first when the condition fails. If Duplicate CMD Bugs is disabled, it will display "hello":
|
[c:\] if 1 == 2 echo Wrong! & echo hello |
Option: