Windows 7 / Getting Started

Conditional Processing with the if Command

The if command lets you run specific commands only if certain conditions are true. If is one of the central requirements of a true programming language, and the extended if command provided by CMD makes the batch file language much more useful than it was in the days of COMMAND.COM.You use the if command in batch files far more often than directly at the command prompt.

The basic formats of the if command are

    if condition command
and
    if condition (command1) else command2

The first version tests some condition and, if it's true, executes command. If the condition is not true, the command is ignored.

The second version provides a second command that is run if the condition is false. In this version, one command or the other is run, depending on the condition. You can group multiple commands in an if statement using parentheses. For example, the statements

if not exist input.dat (
    echo Attention: creating new input.dat file
    dir /b c:\inputs >input.dat
)

check to see whether file input.dat exists and, if it doesn't, run the echo and dir commands. Grouped commands make CMD batch files much easier to read and write than the DOS format, where you would have had to write this instead:

if exist input.dat goto haveinput
    echo Attention: creating new input.dat file
    dir /b c:\inputs >input.dat
:haveinput

You can use the else clause with grouped commands as well, in the following format:

if condition (
    commands
    ...
) else (
    commands
    ...
)

The indenting is optional; it just makes the commands easier to read. There are several condition tests, as listed here:

  • [not] strng1 == strng2
    Compares strng1 to strng2 and is true if the strings are exactly equal, including case.The sense can be reversed by preceding the test with the word not.
  • [not] errorlevel number
    Examines the exit status value of the last program that is run.The condition is true if the exit status is greater than or equal to number. Not reverses the sense; the test is true if the exit status is less than number.
  • [not] exist [path]name
    The test is true if the named file or directory exists.The sense can be reversed with not.To test for a directory, add a backslash (\) to the end of the directory name. For example, if exist c:\temp\ will detect the existence of directory c:\temp, but will not be fooled by a file named c:\temp.
  • [/i] strng1 compareop strng2
    Compares two strings and returns the result of the comparison. If both the strings consist only of digits, the comparison is made numerically; otherwise, it's made alphabetically. If /i is used, the test is case-insensitive. Compareop is one of the following:
    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
  • cmdextversion number
    The test is true if the version number of the CMD extensions is greater than or equal to number.The command extension version number for Windows 7,Vista, and XP is 2.The test is always false if the command extensions are disabled.
  • defined variable
    The test is true if the named environment variable is defined and is not empty.

The extended if statement is one of CMD's biggest improvements. Combined with environment variables, you have a decent programming language.

[Previous] [Contents] [Next]