|
IF
Performs boolean operations in batch files. Performs
conditional processing in batch programs.
IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
NOT Specifies that Windows 2000 should carry
out the command only if the condition is
false.
ERRORLEVEL number Specifies a true condition if the last
program run returned an exit code equal
to or greater than the number specified.
string1==string2 Specifies a true condition if the
specified text strings match.
EXIST filename Specifies a true condition if the
specified filename exists.
command Specifies the command to carry out if
the condition is met. Command can be
followed by ELSE command which will
execute the command after the ELSE
keyword if the specified condition is
FALSE
The ELSE clause must occur on the same line as the command
after the IF.
Use with "errorlevel"
The generic paramater errorlevel refers to the output
another program or command and is also used with the CHOICE
structure. If you try and run a command in a batch file and
produces an error, you can use errorlevel to accept the
returned code and take some action. For example, let's say
you have a batch file that deletes some file.
COPY C:\file.txt C:\file2.txt
If "file.txt" doesn't exist, you will get the error: COULD
NOT FIND C:\FILE.TXT. Instead, use a structure like this to
create the file, then copy it by accepting the error.
@ECHO OFF
:START
COPY file.txt file2.txt
IF errorlevel 1 GOTO MKFILE
GOTO :END
:MKFILE
ECHO file text>file.txt
GOTO START
:END
ECHO Quitting
PAUSE
an errorlevel of 1 means there was an error, errorlevel of 0
means there was no error. You can see these levels by adding
this line after any line of commands:
ECHO errorlevel: %errorlevel%
Batch File Commands
|