Name IF
Abrv
Ver 2.0 ...
Type Int
Allows for conditional execution of a DOS command.
If the conditional expression is true then the command
is executed otherwise (is false) the command is skipped.
Syntax IF expression command parameters
   expression : may optionally have NOT before one of the following
      ERRORLEVEL number
         Tests if exit code (0..255) that a program returns is
         greater than or equal to number (see note 1).
      string1==string2
         Compares two strings.
      EXIST pathname
         Checks for existence of a file or (sub)directory (see note 9).
   command : DOS command to be executed if expression is true
   parameters : optional parameters (arguments and switches)for command

Examples:
   IF ERRORLEVEL 0 GOTO ALWAYS
   IF NOT ERRORLEVEL 5 CALL fail.bat
   IF "%1"=="" GOTO END
   IF NOT "%2=="V1" GOTO V1_
   IF EXIST d:\foo.bat CALL d:\foo.bat
   IF NOT EXIST C:WINDOWS CALL no_os.bat
Notes
1) Not all program return exit codes.
   If a program does not return an exit code then:
      IF ERRORLEVEL 0 command
   evaluates to true, but any other return code value evaluates to false.
   DOS internal commands do not return an exit code, use an envrionment
   variable instead and test it in the calling batch file.
   The following DOS external commands return exit codes:
       BACKUP    CHKDSK   CHOICE   DEFRAG    DELTREE  DISKCMP
       DISKCOPY  FIND     FORMAT   GRAFTABL  KEYB     MOVE
       MSAV      REPLACE  RESTORE  SCANDISK  SETVER   XCOPY
   It is not yet possible to set the exit code for a batch file.
2) To run a batch file as command use one of:
      CALL path_name.bat parameters
      COMMAND /C path_name.bat parameters
   or else control never returns to batch file with IF.
3) IF commands can be nested. This is a way to test for ranges of exit codes:
      IF ERRORLEVEL 1 IF NOT ERROR LEVEL 4 GOTO ONE_TO_THREE
   (i.e. GOTO ONE_TO_THREE if 1<=exit_code<4).
4) The use of @ to suppress printing the conditional command will not work
   because the @ is not the first character in the line. DOS will take the
   @ as part of the command.
5) For the string compare expression, string1==string2, DOS does a case
   sensitive compare. Neither string can have embedded space or equal signs.
6) Environment variables, %environ, or replaceable parameters, %0..%9, are
   substituted when DOS parses each line. DOS does not do these substitutions
   for commands entered at the command line prompt.
7) Each string must have at least one character in it or DOS will give a syntax
   error. In DOS 6 ... string2 can be empty without an error. Two ways
   round these promplems are to double quote everything or to use a additional
   dummy character, e.g. x, instead of: %1==%BIGBIRD%
      "%1"=="%BIGBIRD%"
      %1x==%BIGBIRD%x
8) Using two or more equal signs work but one equal sign is a syntax error.
9) The EXIST pathname expression is true if at least one file matches the
   specified pathname. If non existing drive or path is specified the 
   condition is false. If pathname is omitted the current dierctory is  
   used. DOS does not look in %PATH%. This test is intended for testing for 
   existence of a file but can be used to test for existence of a path if the 
   NUL (or any other valid DOS) device is used:
      IF EXIST path\NUL
See also
GOTO Batch_Files

© 2005 Prem Sobel. All Rights Reserved.