The if statement has a lot of functionality. Common uses include the
following.
if [/i] [not]
if [/i] [not] errorlevel number command [else expression]
if [/i] [not] string1==string2 command [else expression]
if [/i] [not] exist filename command [else expression]
The /i switch, when specified, forces string comparisons to ignore case.
The /i switch can also be used on the string1==string2 form of if. These
comparisons are generic, in that if both string1 and string2 are both
comprised of all numeric digits, then the strings are converted to numbers
and a numeric comparison is performed.
defined variable
The defined conditional works just like exist except it takes an environment
variable name and returns true if the environment variable is defined. Three
variables are added with this conditional: %errorlevel%, %cmdcmdline%, and
%cmdextversion%.
%errorlevel% expands into a string representation of the current value of
errorlevel, provided that there is not already an environment variable with
the name ERRORLEVEL, in which case you will get its value instead. After
running a program, the following illustrates errorlevel use:
The /i parameter makes the comparison case-insensitive. The comparison can
be one of the following.
EQU equal
NEQ not equal
LSS less than
LEQ less than or equal
GTR greater than
GEQ greater than or equal
if errorlevel
if exist file_name
if errorlevel 1 if not errorlevel 2 goto el1
if errorlevel 2 if not errorlevel 3 goto el2
goto el%erorlevel%
:el0
echo Program had return code 0
:el1
echo Program had return code 1
You can also use the comparison operators listed above at compare-op:
if %errorlevel% LEQ 1 goto okay
Using if to verify the presence of a directory:
if exist c:\dir\nul echo c:\dir\ does exist
The if command cannot be used to test directly for a directory, but the null
(NUL) device does exist in every directory. Therefore, you can test for the
null device to determine whether a directory exists.