Share via


If

Applies To: Windows Server 2003, Windows Server 2003 R2, Windows Server 2003 with SP1, Windows Server 2003 with SP2

If

Performs conditional processing in batch programs.

Syntax

if [not] errorlevel Number Command [else Expression]

if [not] String1**==**String2 Command [else Expression]

if [not] exist FileName Command [else Expression]

If command extensions are enabled, use the following syntax:

if [/i] String1 CompareOp String2 Command [else Expression]

if cmdextversion Number Command [else Expression]

if defined Variable Command [else Expression]

Parameters
  • not
    Specifies that the command should be carried out only if the condition is false.
  • errorlevel Number
    Specifies a true condition only if the previous program run by Cmd.exe returned an exit code equal to or greater than Number.
  • Command
    Specifies the command that should be carried out if the preceding condition is met.
  • String1 == String2
    Specifies a true condition only if String1 and String2 are the same. These values can be literal strings or batch variables (for example, %1). You do not need to use quotation marks around literal strings.
  • exist FileName
    Specifies a true condition if FileName exists.
  • CompareOp
    Specifies a three-letter comparison operator. The following table lists valid values for CompareOp.
<table>
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<thead>
<tr class="header">
<th>Operator</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>EQU</p></td>
<td><p>equal to</p></td>
</tr>
<tr class="even">
<td><p>NEQ</p></td>
<td><p>not equal to</p></td>
</tr>
<tr class="odd">
<td><p>LSS</p></td>
<td><p>less than</p></td>
</tr>
<tr class="even">
<td><p>LEQ</p></td>
<td><p>less than or equal to</p></td>
</tr>
<tr class="odd">
<td><p>GTR</p></td>
<td><p>greater than</p></td>
</tr>
<tr class="even">
<td><p>GEQ</p></td>
<td><p>greater than or equal to</p></td>
</tr>
</tbody>
</table>
  • /i
    Forces string comparisons to ignore case. You can use /i on the String1**==**String2 form of if. These comparisons are generic, in that if both String1 and String2 are both comprised of all numeric digits, the strings are converted to numbers and a numeric comparison is performed.
  • cmdextversion Number
    Specifies a true condition only if the internal version number associated with the Command Extensions feature of Cmd.exe is equal to or greater than Number. The first version is 1. It is incremented by one when significant enhancements are added to the command extensions. The cmdextversion conditional is never true when command extensions are disabled (by default, command extensions are enabled).
  • defined Variable
    Specifies a true condition if Variable is defined.
  • Expression
    Specifies a command-line command and any parameters to be passed to the command in an else clause.
  • /?
    Displays help at the command prompt.
Remarks
  • 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 the command executes any command in the else clause (that is, if you specify a command in the else clause).

  • When a program stops, it returns an exit code. To use exit codes as conditions, use errorlevel.

  • Using definedVariable

    If you use definedVariable, the following three variables are added: %errorlevel%, %cmdcmdline%, and %cmdextversion%.

    %errorlevel% expands into a string representation of the current value of errorlevel, provided that there is not an existing environment variable with the name ERRORLEVEL, in which case you get the ERRORLEVEL value instead. The following example illustrates how you can use errorlevel after running a batch program:

    goto answer%errorlevel%
    :answer0
    echo Program had return code 0
    :answer1
    echo Program had return code 1
    goto end
    :end
    echo done! 
    

    You can also use the CompareOp comparison operators as follows:

    if %errorlevel% LEQ 1 goto okay
    

    %cmdcmdline% expands into the original command line passed to Cmd.exe prior to any processing by Cmd.exe, provided that there is not an existing environment variable with the name CMDCMDLINE, in which case you get the CMDCMDLINE value instead.

    %cmdextversion% expands into the a string representation of the current value of cmdextversion, provided that there is not an existing environment variable with the name CMDEXTVERSION, in which case you get the CMDEXTVERSION value instead.

  • Using the else clause

    You must use the else clause on the same line as the command after the if. For example:

    IF EXIST FileName. (
    del FileName.
    ) ELSE (
    echo FileName. missing.
    )
    

    The following code does not work because you must terminate the del command by a new line:

    IF EXIST FileName. del FileName. ELSE echo FileName. missing
    

    The following code does not work because you must use the else clause on the same line as the end of the if command:

    IF EXIST FileName. del FileName.
    ELSE echo FileName. missing
    

    If you want to format it all on a single line, use the following form of the original statement:

    IF EXIST FileName. (del FileName.) ELSE echo FileName. missing
    
Examples

To display the message "Cannot find data file" if the file Product.dat cannot be found, type:

if not exist product.dat echo Cannot find data file 

If an error occurs during the formatting of the disk in drive A, the following example displays an error message:

:begin
@echo off
format a: /s
if not errorlevel 1 goto end
echo An error occurred during formatting.
:end
echo End of batch program.

If no error occurs, the error message does not appear.

Formatting legend

Format Meaning

Italic

Information that the user must supply

Bold

Elements that the user must type exactly as shown

Ellipsis (...)

Parameter that can be repeated several times in a command line

Between brackets ([])

Optional items

Between braces ({}); choices separated by pipe (|). Example: {even|odd}

Set of choices from which the user must choose only one

Courier font

Code or program output

See Also

Concepts

Using batch files
Cmd
Command-line reference A-Z
Command shell overview