IF

Performs conditional processing in batch programs.

IF [NOT] {ERRORLEVEL number | CMDEXTVERSION number | [/I] string1 op string2 | EXIST filename | DEFINED variable} (command1) [ELSE command2]

Where:

  • NOT - Specifies that Windows runs the command only if the condition is false.
  • ERRORLEVEL number - Condition is true if the last executed program returned an exit code equal to or greater than the specified number.
  • CMDEXTVERSION number - Condition is true if the command extensions version is equal to or greater than number. In general, number is 1 if command extensions are disabled and 2 if they are enabled (which is the default value).
  • /I - Specifies not to be case-sensitive when comparing strings.
  • string1 op string2 - Condition is true if the comparison is respected. op is the comparison operator:
    • == - equal to.
    • EQU - equal to.
    • NEQ - not equal to.
    • LSS - less than.
    • LEQ - less than or equal to.
    • GTR - greater than.
    • GEQ - greater than or equal to.
  • EXIST filename - Condition is true if the specified filename exists.
  • DEFINED variable - Condition is true if the specified environment variable is defined.
  • command1 - Command to execute if the condition is met.
  • ELSE command2 - Command to execute if the condition is not met.

The syntax can also be:

IF [NOT] {ERRORLEVEL number | CMDEXTVERSION number | [/I] string1 op string2 | EXIST filename | DEFINED variable} (
  command1
) [ELSE (
  command2
)]

Where you can put multiple conditional commands.

Examples:

1. Executes the ECHO Hello! command if the string everest is same as EVEREST:

if everest==EVEREST (echo Hello!)

or:

if everest==EVEREST (
    echo Hello!
)

In this case, the command will not be executed because, without the /I option, uppercase and lowercase letters are distinguished.

 

2. Execute the command "C:\Ciccio.txt" and the command ECHO Opening C:\Ciccio.txt completed. if the file C:\Ciccio.txt exists, otherwise execute ECHO C:\Ciccio.txt was not found.:

if /i exist "C:\Ciccio.txt" (
    "C:\Ciccio.txt"
    echo Opening C:\Ciccio.txt completed.
) else (
    echo C:\Ciccio.txt was not found.
)

 

3. Executes ECHO Environment variable var is equal to %var%. if the var variable is defined, otherwise ECHO Environment variable var is not defined.:

if defined var (
    echo Environment variable var is equal to %var%.
) else (
    echo Environment variable var is not defined.
)

 

Further information:

To expand the value of an environment variable, enclose the variable name in percent signs %, as in the example (see ECHOECHO):

echo %path%

Displays the value of the PATH environment variable (see PATHPATH). This applies to all defined variables, including the MS-DOS defaults, such as:

  • %ERRORLEVEL% - Expands the exit code of the last instruction.
  • %CMDCMDLINE% - Expands the command prompt path.
  • %DATE% - Current date (see DATEDATE).
  • %TIME% - Current time (see TIMETIME).
  • %CD% - Current directory (see CD or CHDIRCD or CHDIR).
  • %RANDOM% - Random number between 0 and 32767.

Comments