home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / doshelp / helpsb.zoo / help / batch / if / examples.hlp < prev    next >
Encoding:
Text File  |  1989-01-01  |  2.0 KB  |  57 lines

  1. SAMPLE command and resulting output   | Comments about the output
  2.  
  3.    IF EXIST T.SAV COPY T.SAV \T.NEW    If a file named T.SAV exists in the
  4.                     current directory, a copy called 
  5.                     T.NEW will be made in the root 
  6.                     directory.
  7.  
  8.    IF NOT EXIST %1 IF EXIST %1\NUL GOTO ITSDIRECT
  9.                     If replaceable parameter %1 is a
  10.                     directory (empty or not), this test 
  11.                     will succeed. If %1 is a file, or 
  12.                     does not exist, this test will fail.
  13.                     Using *.* instead of NUL, this test
  14.                     would fail for an empty directory.
  15.  
  16.   IF X==X%2 ECHO Name missing        If the second replaceable parameter
  17.   IF NOT X==%2X GOTO NAMEGOOD        was absent when the BATCH file 
  18.                     containing this command was 
  19.                     executed, "Name missing" would be 
  20.                     displayed, and the GOTO would not 
  21.                     be executed. If %2 had any internal
  22.                     spaces, both IF commands would fail.
  23.                     However, if %2 has leading spaces, or
  24.                       was all spaces, the test 
  25.  
  26.                         IF X==%2X 
  27.  
  28.                     would fail.
  29.  
  30.   IF  Hello==%1 GOTO NEWUSER        In a BATCH file executed with the
  31.                     first replaceable parameter equal
  32.                     Hello, the GOTO would be executed. 
  33.                     If the first replaceable parameter 
  34.                     were anything else, the GOTO would 
  35.                     not be executed.
  36.  
  37. IF %SWITCHAR%==- DIR -W %1        If the DOS option character has been
  38.   IF %SWITCHAR%==/ DIR /W %1        changed to "-", execute the DIR 
  39.                     command with the -W option. If it
  40.                     is the normal "/", execute the DIR
  41.                     command with the /W option. Note that
  42.                     a more compact way to do this is
  43.  
  44.                        DIR %SWITCHAR%W %1
  45.  
  46.   IF ERRORLEVEL 3 IF NOT ERRORLEVEL 4 EXIT Since the ERRORLEVEL condition tests
  47.                     for greater than or equal to, and IF
  48.                     commands can be nested, this is how
  49.                     to test ERRORLEVEL equal to 3, and 
  50.                     if it is, to EXIT the batch file.
  51.  
  52.   IF NOT ERRORLEVEL 2 EXIT        IF ERRORLEVEL less than 2, EXIT the
  53.                     batch file.
  54.  
  55.   IF NOT ERRORLEVEL 1 ECHO Success!    IF ERRORLEVEL equal 0, display
  56.                     Success! on the screen.
  57.