IF
Conditionally perform a command.
File syntax
IF [NOT] EXIST filename command IF [NOT] EXIST filename (command) ELSE (command) String syntax
IF [/I] [NOT] item1==item2 command IF [/I] item1 compare-op item2 command IF [/I] item1 compare-op item2 (command) ELSE (command) Error Check Syntax IF [NOT] DEFINED variable command IF [NOT] ERRORLEVEL number command IF CMDEXTVERSION number command key item : May be a text string or an environment variable a variable may be modified using either
Substring syntax or Search syntax command : The command to perform NOT : perform the command if the condition is false. == : perform the command if the two strings are equal. /I : Do a case Insensitive string comparison. compare-op : may be one of EQU : equal NEQ : not equal LSS : less than < LEQ : less than or equal <= GTR : greater than > GEQ : greater than or equal >= This 3 digit syntax is necessary because the > and < are recognised as redirection symbols
IF EXIST filename will return true if the file exists (this is
not case sensitive).
IF ERRORLEVEL statements should be read as IF Errorlevel > OR = number
i.e.
IF ERRORLEVEL 1 will return TRUE for an errorlevel of 1 or greater.
To put that another way, ERRORLEVEL will return 0 on the successful completion of a command, however IF ERRORLEVEL 0 will also return true even if the errorlevel is 196!
Examples:
IF EXIST C:\install.log (echo complete) ELSE (echo failed) IF DEFINED _department ECHO Got the department variable IF DEFINED _commission SET /A _salary=%_salary% + %_commission% IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2
Does %1 exist?
To test for the existence of a command line paramater - use empty brackets like this
IF [%1]==[] ECHO Value Missing
or
IF [%1] EQU [] ECHO Value Missing
In the case of a variable that may be NULL - a null variable will remove the variable definition altogether, so testing for NULLs becomes easy:
IF NOT DEFINED _example ECHO Value Missing
IF DEFINED will return true if the variable contains any value (even if the value is just a space)
Test the existence of files and folders
IF EXIST name - will detect the existence of a file or a folder - the script empty.cmd will show if the folder is empty or not.
Brackets
You can improve the readability of a batch script by writing a complex IF...ELSE command over several lines using brackets
e.g.
IF EXIST filename (
del filename
) ELSE (
echo The file was not found.
)
The IF statement does not use any great intelligence when evaluating Brackets, so for example the command below will fail:
IF EXIST MyFile.txt (ECHO Some(more)Potatoes)
This version will work:
IF EXIST MyFile.txt (ECHO Some[more]Potatoes)
Testing Numeric values
Do not use brackets or quotes when comparing numeric values
e.g.
IF (2) GEQ (15) echo "bigger"
or
IF "2" GEQ "15" echo "bigger"
These will perform a character comparison and will echo "bigger"
however the command
IF 2 GEQ 15 echo "bigger"
Will perform a numeric comparison and works as expected - notice that this behaviour is exactly opposite to the SET /a command where quotes are required.
Any test made using the compare-op syntax will always be a "string" comparison,
so when comparing numbers note that "026" > "26"
Wildcards
Simple wildcards are not supported by IF, so ==SS6* will not match SS64
The workaround is to spoof a wildcard using SET to retrieve the substring
SET _part_name=%COMPUTERNAME:~0,3%
IF NOT %_part_name%==SS6 GOTO they_matched
Pipes:
When piping commands, the expression is evaluated from left to right, so
IF... | ... is equivalent to (IF ... ) | ...
you can use the explicit syntax IF (... | ...)
Setting an ERRORLEVEL
It is possible to create a string variable called %ERRORLEVEL%
(user variable)
if present such a variable will prevent the real ERRORLEVEL (a system variable) from being used by commands such as ECHO and IF.
If you want to deliberately raise an ERRORLEVEL in a batch script use the command "COLOR 00" or simply SET MyError=YES
To test for the existence of a user variable use SET errorlevel, or IF DEFINED ERRORLEVEL
If Command Extensions are disabled IF will only support direct comparisons: IF ==, IF EXISTS, IF ERRORLEVEL
also the system variable CMDEXTVERSION will be disabled.
You see things; and you say 'Why?' But I dream things that never were; and I say 'why not?' - George Bernard Shaw
Related commands:
Conditional execution syntax (AND
/ OR)
SET - Display, or Edit Windows NT environment variables
ECHO - Display message on screen
IFMEMBER - NT Workgroup member (Resource kit)
SC - Is a Service running (Resource kit)
Equivalent Linux BASH commands:
case - Conditionally perform a command
if - Conditionally perform a command
until - Execute commands (until error)
while - Execute commands