Index | Syntax Errorlevel | Batch



IF--Examples

The following 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 The following example displays the message "Can't find data file" if MS-DOS 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 goto NoSuch Errorlevel When a program stops, it returns an exit code to MS-DOS. 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. The following 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. :begin echo off format a: /s if not errorlevel 1 goto end echo An error occurred during formatting. :end echo End of batch program. For another example of how the ERRORLEVEL parameter is used, see the <CHOICE> command. IF and OR: for %%p in (' '0 '00) do if %%p=='%v% echo Parameter v: (%%v%%) = (%v%). :: The following batch program returns the errorlevel parameter. :: February 16, 2001. (See an updated version from August 2001). :: @ECHO OFF SET E= FOR %%! IN (1 2) DO IF ERRORLEVEL %%!00 SET E=%%! SET !=0 1 2 3 4 5 6 7 8 9 IF ERRORLEVEL ø SET !=0 1 2 3 4 5 IF ERRORLEVEL : FOR %%! IN (%!%) DO IF ERRORLEVEL %E%%%!0 SET E=%E%%%! IF NOT ERRORLEVEL * SET !=0 1 2 3 4 5 6 7 8 9 FOR %%! IN (%!%) DO IF ERRORLEVEL %E%%%! SET E=%E%%%! ECHO Errorlevel = %E% FOR %%! IN (! E) DO SET %%!= Notes: A char of ASCII value 248 is used instead of 200, colon is used instead of 10 and * instead of 250. To prefix with 0: delete line number 2, "SET E=", substitute line number 3, "(1 2)" with: "(0 1 2)" and line number 6: delete the "IF ERRORLEVEL : ". Links: Set and test an Errorlevel parameter. Get Errorlevel ::1 SET E= ::2 SET E= % two spaces % FOR %%! IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL %%!0 SET E=%%! rem --------^----------------------------------------------^ FOR %%! IN (10 11 12 13 14 15 16 17 18 19 20 21 22 23 24) DO IF ERROR LEVEL %%!0 SET E=%%! FOR %%! IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL %E%%%! SET E=%E%%%! FOR %%! IN (0 1 2 3 4 5 ) DO IF ERRORLEVEL 25%%! SET E=25%%! ECHO. Errorlevel is %E% Modifications: The program/tool works as it is but it can be modified as this: -- Align LEFT: Remove the ::1 in line num 1 & the first zero in line num 3. -- Align RIGHT: Substitute in line num 3: the (SET E=%%!) with (SET E= %%!), & then either remove the (::2 ) OR substitute the first zero (0 ) in line num 3 with (" "), i.e. Quote, Space and Quote. If the (::2 ) is removed, then you may also remove the zero. -- Three digits i.e.: both left and right aligned: Substitute in line num 3: the (SET E=%%!) with (SET E=0%%!). Notes: As it is, OR if the modification for left alignment is done, then all the elements in the first FOR loop can be removed into the second/next loop. The longest line was wrapped so, either unwrap the second FOR loop or split the whole line as this: FOR %%! IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL 1%%!0 SET E=1%%! FOR %%! IN (0 1 2 3 4 ) DO IF ERRORLEVEL 2%%!0 SET E=2%%! -- Notis: If you split the second FOR loop as shown above, then you can't remove the elements as described in the first note.
-Top- | Syntax Batch