If Then ElseIf
Microsoft® Windows® 2000 Scripting Guide
System administrators often work in an either-or world: Either a computer has enough disk space or it does not; either a computer has enough available memory or it does not. In situations in which there are only two possible conditions either a computer has enough free disk space or it does not - the If Then Else construction allows you to test the condition and choose a course of action, with a minimal amount of coding.
At other times, however, there can be more than two possible conditions. For example, you might assess maintenance requirements for disk drives by using the following criteria:
A drive with less than 100 megabytes of free space requires immediate attention.
A drive with less than 250 megabytes of free space but more than 100 megabytes of free space should be looked at as time allows.
A drive with more than 250 megabytes of free space requires no attention at this time.
To evaluate multiple possibilities, you can use an If Then ElseIf statement. With an If Then ElseIf statement in your script, the script checks for the first condition (for example, x = 1). If true, the script takes the specified action and then exits the If Then construct.
But what if the condition is false and x does not equal 1? In that event, the script can check for a second condition by using a syntax similar to this:
ElseIf x = 2 Then
You can continue inserting ElseIf statements until you have exhausted all the possible values for x. (Actually, to ensure that you have all the possible values covered, your last statement should be simply an Else statement. The Else statement can be translated as, "If none of the preceding conditions are true, then take this action.")
For example, the following lines of code echo a message depending on whether:
FreeMegabytes is less than or equal to 100.
FreeMegabytes is greater than 100 but less than 250.
FreeMegabytes is equal to or greater than 250.
If FreeMegabytes <= 100 Then Wscript.Echo Computer.Name & " requires immediate attention." ElseIf FreeMegabytes < 250 Then Wscript.Echo Computer.Name & " should be looked at as soon as possible." Else Wscript.Echo Computer.Name & " requires no attention at this time." End If
When evaluating multiple possibilities using either an If Then ElseIf or a Select Case statement (discussed in the next section of this chapter), VBScript checks each condition until it finds a True statement. At that point it takes action without evaluating any of the other conditions. That means it is very important to properly order your conditional statements.
For example, consider the following script sample:
x = 5 If x < 20 Then Wscript.Echo "X is between 11 and 20." ElseIf x < 11 Then Wscript.Echo "X is between 0 and 10." Else Wscript.Echo "X equals 0." End If
Figure 2.19 shows the message box that is displayed when you run this script.
Figure 2.19 Incorrectly Ordering Conditional Statements
Why is the incorrect result? The first condition checked in the If Then ElseIf statement is this: Is X less than 20? Because that happens to be true, VBScript echoes the message, "X is between 11 and 20" and then immediately exits the statement block. The script never checks to see whether X is less than 11 because processing ends as soon as a True statement is found.
To get the script to work properly, the conditions must be reordered like this:
x = 5 If x = 0 Then Wscript.Echo "X equals 0." ElseIf x < 11 Then Wscript.Echo "X is between 0 and 10." Else Wscript.Echo "X is between 11 and 20." End If
It is generally a good idea to always include an Else statement as the last statement within the If Then block; this enables your script to account for all conditions, not just the ones specified in the ElseIf statement. For example, consider the following script snippet, which matches a job ID number with a job title:
If JobID = 1 Then Wscript.Echo "This user is a manager." ElseIf JobID = 2 Then Wscript.Echo "This user is not a manager." End If
The preceding script works fine as long as each user has been assigned a job ID of 1 or 2. But what if the user was assigned a job ID of 3, or not assigned a job ID? By adding an Else statement, you can account for any job ID that is not 1 or 2:
If JobID = 1 Then Wscript.Echo "This user is a manager." ElseIf JobID = 2 Then Wscript.Echo "This user is not a manager." Else Wscript.Echo "This user has not been assigned a valid job ID." End If