Performs conditional processing in batch programs. If the condition specified by an IF command is true, it carries out the command that follows the condition. If the condition is false, it ignores the command. You can use this command only in batch programs.
IF /?
Error Check Syntax:
IF [NOT] ERRORLEVEL nnn command
IF [NOT] DEFINED variable command
IF CMDEXTVERSION nnn command
String syntax:
IF [/I] [NOT] string1==string2 command
IF [/I] item1 compare-op item2 command
IF [/I] item1 compare-op item2 (command) ELSE (command)
File syntax:
IF [NOT] EXIST filename command
IF [NOT] EXIST filename (command) ELSE (command)
SET - Display, or Edit 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.
eval - Evaluate several COMMANDS/arguments.
if - Conditionally perform a command.
m4 - Macro processor.
until - Execute commands (until error).
while - Execute commands.
Parenthesis:
The use of parenthesis is only required if the command is run over several lines:
IF EXIST filename ( DEL filename ) ELSE ( echo The file was not found. )
When piping commands, the expression is evaluated from left to right, so:
IF... | ... is equivalent to (IF ... ) | ...
you can use the explicit syntax IF (... | ...)
Testing ERRORLEVEL correctly:
It is possible to create a string variable called %ERRORLEVEL% if present such a variable will prevent the real ERRORLEVEL from being tested successfully.
Both ECHO %errorlevel% and IF %errorlevel% will respond to a string variable %ERRORLEVEL% in preference to a real ERRORLEVEL.
To test for this problem use SET errorlevel, or IF DEFINED ERRORLEVEL.
If you want to deliberately raise an ERRORLEVEL in a batch script use the command "COLOR 00".
If Command Extensions are disabled IF will only support direct comparisons: IF ==, IF EXISTS, IF ERRORLEVEL.
This example tests for the existence of a directory. The IF command cannot be used to test directly for a directory, but the null (NUL) device does exist in every directory on the hard drive. Therefore, you can test for the null device to determine whether a directory exists on the hard drive.
IF EXIST C:\MYDIR\NUL GOTO PROCESS
Joachim Proske says you CAN test directly for a directory (at least under NT2003:
IF NOT EXIST D:\TEMP\TESTING MD D:\TEMP\TESTING).
Joachim Proske also says you CAN test UNC directories too (at least under NT2003:
IF NOT EXIST \\DEFRMFS2\TEST\LOG\TESTING MD \\DEFRMFS2\TEST\LOG\TESTING).
This example displays the message 'Can't find data file' if the operating system cannot find the file PRODUCT.DAT:
IF NOT EXIST PRODUCT.DAT ECHO Can't find data file
Using a variable in EXIST and GOTO syntax:
IF NOT EXIST %FILE% GOTO NOSUCH
Errorlevel When a program stops, it returns an exit code to the operating system. For example, a value of 0 is typically used to indicate that a program was successfully executed. The ERRORLEVEL parameter lets you use exit codes as conditions.
This example displays an error message if an error occurs during formatting of the disk in drive A. If no error occurs, the error message is skipped.
@ECHO OFF FORMAT A: /S IF NOT ERRORLEVEL 1 GOTO ERR ECHO An error occurred during formatting. GOTO END :ERR ECHO Successfully formatted the disk in drive A. :END
For another example of how the ERRORLEVEL parameter is used, see the CHOICE examples.
IF EXIST C:\install.log (ECHO Installation complete) ELSE ECHO The Installation failed) IF DEFINED v_department ECHO Got the department variable IF DEFINED v_commission SET /A v_salary=%v_salary% + %v_commission% IF CMDEXTVERSION 1 GOTO :start_process IF ERRORLEVEL EQU 0 GOTO :okay :: Capture a specific errorlevel like this FOR %%G IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL==%%G SET v_err=%%G IF [%v_err%]==[2] ECHO Errorlevel is 2 :: Wildcards are not supported by IF but you can spoof the same thing using SET: :: This won't Work IF %COMPUTERNAME%==ABZ* GOTO :s_matched :: This will work SET v_part_name=%COMPUTERNAME:~0,3% IF NOT %v_part_name%==ABZ GOTO :s_matched
none.