I'm trying to understand the syntax of IF in batch files.
Given this code (works):
CHOICE /C YN /M "Do it?"
IF ERRORLEVEL == 2 GOTO skip
IF ERRORLEVEL == 1 GOTO doIt
GOTO end
:doIt
echo Do it!
GOTO end
:skip
echo Abort!
GOTO end
:end
Why can't I change the order of the two IF's? If I would write IF ERRORLEVEL == 1 GOTO doIt at first, I get wrong behavior. Now Do it gets executed every time, regardless of the input.
The
ifcommand supports a few special (case-insensitive) keywords:exist(to check for file existence)defined(to check for environment variable)ErrorLevel(to check for the last error)CmdExtVersion(to check for command extensions)If any of those is encountered immediately behind
if,if /I,if notorif /I not, special comparison modes are entered. If none of these keywords is present, a normal comparison of two values is expected (using the comparison operator==to force string comparison, or using one ofequ,neq,gtr,geq,lss,leqfor trying to interpret both values as integers and comparing them as such, or, if not possible, comparing them as strings).Since you have stated the keyword
errorlevelimmediately following theifcommand, a numeric value is expected. The equal-to sign is no longer treated particularly, rather is it just regarded as a standard token delimiter just like a SPACE, according to this section1, and multiple consecutive delimiters are collapsed into one.Therefore, your command line
if errorlevel == #is equivalent toif errorlevel #, meaning ifErrorLevelis greater than or equal to#. For that reason, you cannot exchange the twoifcommand lines, because anerrorLevelvalue of2would also fulfil said condition against the value1.1) Actually, the vertical tabulator (code
0x0B) and the non-break space (code0xFF) are missing in this list.