Perform conditional processing in batch programs.
IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
|NOT||Specifies that Windows NT should carry out the command only if the condition is false.|
|ERRORLEVEL number||Specifies a true condition if the last program run returned an exit code equal to or greater than the number specified.|
|command||Specifies the command to carry out if the condition is met.|
|string1==string2||Specifies a true condition if the specified text strings match.|
|EXIST filename||Specifies a true condition if the specified filename exists.|
|MS-DOS and PC-DOS
(including Win95's MS-DOS 7)
|as described above|
|OS/2 Warp||as described above|
|Windows NT 4/2000/XP||special features: compare numbers, case insensitive matching, and more|
|1||When comparing strings, make sure neither of them is empty or contains spaces only (both leading and trailing spaces are ignored).
Empty strings can be prevented easily by adding some meaningless but non-empty string at both sides of the equal sign:
Even if no parameter is given (in which case %1 is an empty string) the string before the equal sign won't be empty.
A commonly used way to prevent empty strings is enclosing them in either quotes or (square) brackets:
In NT you can strip doublequotes using a tilde:
So in NT you would use:
|2||Matches should be exact; none of the strings "abc", "Abc" and "ABC" match.
Only NT provides case insensitive matches using the /I option:
|3||In "real" DOS (COMMAND.COM),
Since DOS' devices AUX, COMn, LPTn, NUL and PRN are also treated as files, and they exist in every directory, one way to check for the existence of a directory would be:
Note, however, that you will lose one file handle for each directory you perform this check on, since this check opens the (NUL) device using a file handle, but the device will never be closed again until the next reboot.
Note also, that NUL doesn't exist on every type of drive in every DOS version (especially not in Windows 2000 and later, when using junctions or symbolic links).
If all you want to do is create a directory if it does not exist, use
In NT you can check if a directory exists like you would check for the existence of a file:
If you want to check for directories only (in NT), use a trailing backslash:
|4||When checking errorlevels, keep in mind that checking for errorlevel 3 will return TRUE if the errorlevel is 3 or higher!
If you want to check if an errorlevel is exactly equal to 3, use
This will return TRUE if both conditions (greater than or equal to 3 AND NOT greater than or equal to 4) are met, in this case only if the errorlevel is exactly 3.
In NT you can use
|5||Most (but not all) OS/2 and NT commands return a non-zero return code (errorlevel) on failure.
Don't count on it in DOS, though.
page last uploaded: 2018-12-20, 10:48