To get more help on the if statement, type on the command line prompt:
help if
IF condition (
command
) ELSE (
command
)
where:
When using the else construct:
IF condition (command) ELSE command
IF condition command
[/I] [NOT] string1==string2
and
[/I] string1 compare-op string2
where
@echo off
set hello=hello
if "%hello%"=="hello" echo %hello% world
hello world
if 2 GTR 1 (echo Yes) else echo No
Yes
[NOT] EXIST filename
where:
@echo off
if exist hello.bat (
echo hello.bat exist
)
hello.bat exist
Syntax:
ERRORLEVEL number
This condition will return a true condition if the last program run returned an exit code (Errorlevel) equal to or greater than the number specified. When a command executes without error it terminates with an exit status of zero.
With the string comparison
[NOT] %ERRORLEVEL% [compare-op] number
where:
Syntax:
[NOT] DEFINED variableName
Example:
if not defined v (echo v is not defined) else echo %v%
v is not defined
set v=Call Me Nico
if not defined v (echo v is not defined) else echo %v%
Call Me Nico
This error occurs really often when you test a condition over a variable that is not set.
The command interpreter first replace the variable with its value before starting the command and the result is an error in the syntax as the variable has no value.