-------------------------- MS-DOS v6.22 Help: If ---------------------------
<Examples>                                                         <Index>


Performs conditional processing in batch programs. If the condition
specified by an IF command is true, MS-DOS carries out the command that
follows the condition. If the condition is false, MS-DOS ignores the
command. You can use this command only in batch programs.


    IF [NOT] ERRORLEVEL number command

    IF [NOT] string1==string2 command

    IF [NOT] EXIST filename command


    Specifies that MS-DOS should carry out the command only if the condition
    is false.

    Specifies a true condition only if the previous program run by
    COMMAND.COM returned an exit code equal to or greater than number.

    Specifies the command that MS-DOS should carry out if the preceding
    condition is met.

    Specifies a true condition only if string1 and string2 are the same.
    These values can be literal strings or batch variables (%1, for
    example). Literal strings do not need quotation marks.

EXIST filename
    Specifies a true condition if filename exists.




The following example tests for the existence of a directory. The IF command
cannot be used to test directly for a directory, but the null (NUL) device
does exist in every directory on the hard drive. Therefore, you can test for
the null device to determine whether a directory exists on the hard drive.

    if exist c:\mydir\nul goto process

The following example displays the message "Can't find data file" if MS-DOS
cannot find the file PRODUCT.DAT:

    if not exist product.dat echo Can't find data file

When a program stops, it returns an exit code to MS-DOS. For example, a
value of 0 is typically used to indicate that a program was successfully
executed. The ERRORLEVEL parameter lets you use exit codes as conditions.

The following example displays an error message if an error occurs during
formatting of the disk in drive A. If no error occurs, the error message is

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

For another example of how the ERRORLEVEL parameter is used, see the
<CHOICE> command.


<Top of page>
Last update: December 07, 2002 14:45 by
Content © 1997 Microsoft Corporation
All else © 2000-2005