If
Syntax
if [not] String1==String2 Command [else Expression]
if [not] exist FileName Command [else Expression]
If command extensions are enabled, use the following syntax:
if [/i] String1 CompareOp String2 Command [else Expression]
ifcmdextversionNumberCommand [else Expression]
ifdefinedVariableCommand [else Expression]
Parameters
Remarks
- If the condition specified in an if command is true, the command that follows the condition is carried out. If the condition is false, the command in the if clause is ignored and the command executes any command in the else clause (that is, if you specify a command in the else clause).
- When a program stops, it returns an exit code. To use exit codes as conditions, use errorlevel.
-
Using definedVariable
If you use definedVariable, the following three variables are added: %errorlevel%, %cmdcmdline%, and %cmdextversion%.
%errorlevel% expands into a string representation of the current value of errorlevel, provided that there is not an existing environment variable with the name ERRORLEVEL, in which case you get the ERRORLEVEL value instead. The following example illustrates how you can use errorlevel after running a batch program:
goto answer%errorlevel% :answer0 echo Program had return code 0 :answer1 echo Program had return code 1 goto end :end echo done!
You can also use the CompareOp comparison operators as follows:
if %errorlevel% LEQ 1 goto okay
%cmdcmdline% expands into the original command line passed to Cmd.exe prior to any processing by Cmd.exe, provided that there is not an existing environment variable with the name CMDCMDLINE, in which case you get the CMDCMDLINE value instead.
%cmdextversion% expands into the a string representation of the current value of cmdextversion, provided that there is not an existing environment variable with the name CMDEXTVERSION, in which case you get the CMDEXTVERSION value instead.
-
Using the else clause
You must use the else clause on the same line as the command after the if. For example:
IF EXIST FileName. ( del FileName. ) ELSE ( echo FileName. missing. )
The following code does not work because you must terminate the del command by a new line:
IF EXIST FileName. del FileName. ELSE echo FileName. missing
The following code does not work because you must use the else clause on the same line as the end of the if command:
IF EXIST FileName. del FileName. ELSE echo FileName. missing
If you want to format it all on a single line, use the following form of the original statement:
IF EXIST FileName. (del FileName.) ELSE echo FileName. missing
Examples
if not exist product.dat echo Cannot find data file
If an error occurs during the formatting of the disk in drive A, the following example displays an error message:
:begin @echo off format a: /s if not errorlevel 1 goto end echo An error occurred during formatting. :end echo End of batch program.
If no error occurs, the error message does not appear.
Formatting legend
Format | Meaning |
Italic | Information that the user must supply |
Bold | Elements that the user must type exactly as shown |
Ellipsis (...) | Parameter that can be repeated several times in a command line |
Between brackets ([]) | Optional items |
Between braces ({}); choices separated by pipe (|). Example: {even|odd} | Set of choices from which the user must choose only one |
Courier font | Code or program output |