Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
IF--Examples
◄Syntax►
────────────────────────────────────────────────────────────────────────────
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
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.
♦