Saturday, 2 February 2013

IF

The IF subcommand allows you to make decisions within your batch file. Its syntax is:
IF Condition Command where,
Command = Any legal DOS command or batch file subcommand
Condition = One of three tests that yield true or false:
1. The ERRORLEVEL of a program.
2. Two strings are equivalent.
3. A file exists in the current directory.
Unlike programming languages, which allow many logical tests in an IF statement, the batch
IF statement is limited to only the three above.
Condition 1: ERRORLEVEL is a number that indicates to DOS whether the last program
run was successful.

A zero (0) indicates a good run, anything above zero indicates an error condition. (Only
DOS commands BACKUP and RESTORE have an exit code.)
You can create small programs to capture the keyboard output and report it as an error level
(see Batch Sample #3 later).
Condition 2: String comparison is indicated by double equal signs:
String1 == String2
compares the two strings.
This comparison is often used with parameters and markers to check for a particular entry.
For example,
IF %1 == 40 MODE C40
checks parameter one for 40 and, if found, changes the display to 40-columns wide.
Condition 3: The logical test checking for a file has the format:
EXIST d:Filename
You can use this test to check and see if a DOS disk is in the active drive (as one example).
Another use might be to check for a particular file to see if the user has a valid disk in the drive.
Pathnames are NOT allowed.

No comments:

Post a Comment