Freigeben über


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.

Operator

Description

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

/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 executes any command in the else clause, if one has been specified.

  • When a program stops, it returns an exit code. You can use exit codes as conditions by using the errorlevel parameter.

  • Using defined variable 

    If you use defined variable, 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 already an 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:

<pre IsFakePre="true" xmlns="http://www.w3.org/1999/xhtml">if %errorlevel% LEQ 1 goto okay</pre>


**%cmdcmdline%** expands into the original command line passed to Cmd.exe prior to any processing by Cmd.exe, provided that there is not already an 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 already an 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:

<pre IsFakePre="true" xmlns="http://www.w3.org/1999/xhtml">IF EXIST filename. del filename. ELSE echo filename. missing</pre>


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

<pre IsFakePre="true" xmlns="http://www.w3.org/1999/xhtml">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:

<pre IsFakePre="true" xmlns="http://www.w3.org/1999/xhtml">IF EXIST filename. (del filename.) ELSE echo filename. missing</pre>

Examples

If the file Product.dat cannot be found, the following message appears:

if not exist product.dat echo Can't 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.

You cannot use the if command to test directly for a directory, but the null (NUL) device does exist in every directory. As a result, you can test for the null device to determine whether a directory exists. The following example tests for the existence of a directory:

if exist c:\mydir\nul goto process

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

Batch files

Cmd

Command-line reference A-Z