Previous Page
Next Page

If...Then...Else

It is important to point out here that you can use If...Then...Else without the intervening ElseIf commands. In such a construction, you give the script the ability to make a choice between two options.

Just the Steps

To use If...Then...Else

1.
On a new line in the script, type If some condition Then.

2.
On the next line, enter the command you want to invoke.

3.
On the next line, type Else.

4.
On the next line, type the alternate command you want to execute when the condition is not true.

5.
On the next line, type End If.


The use of If...Then...Else is illustrated in the following code:

ifThenElse.vbs

Option Explicit
On Error Resume Next
Dim a,b,c,d
a = 1
b = 2
c = 3
d = 4
If a + b = d Then
  WScript.Echo (a & " + " & b & " is equal to " & d)
Else
  WScript.Echo (a & " + " & b & " is equal to " & c)
End If

In the preceding ifThenElse.vbs script, you declare your four variables on one line. You can do this for simple scripts such as this one. It can also be done for routine variables that are associated with one another, such as objWMIService and objProcessor from your earlier script. The advantage of putting multiple declarations on the same line is that it makes the script shorter. Although this does not really have an impact on performance, it can at times make the script easier to read. You'll need to make that calldoes making the script shorter make the script easier to read, or does having each variable on a separate line with individual comments make the script easier to read?

When you do the WScript.Echo command, you're using a feature called concatenation, which puts together an output line by using a combination of variables and string text. Notice that everything is placed inside the parentheses and that the variables do not go inside quotation marks. To concatenate the text into one line, you can use the ampersand character (&). Because concatenation does not automatically include spaces, you have to put in the appropriate spaces inside the quotation marks. By doing this, you can include a lot of information in the output. This is one area that requires special attention when you're modifying existing scripts. You might need to change only one or two variables in the script, but modifying the accompanying text strings often requires the most work.

Using If...Then...Else to fix the syntax of output

1.
Open the QueryAllProcessors.vbs script in the folder \My Documents\Microsoft Press\VBScriptSBS\ch03 using Notepad or the script editor of your choice. Save it as YourNameQueryAllProcessorsSyntax.vbs.

2.
Put a new function definition at the end of the script. (We will discuss user defined functions in just a few pages.) Use the word Function and give it the name funIS. Assign the input parameter the name intIN. The syntax for this line will look like the following:

Function funIS(intIN)

3.
Space down a few lines and end the function with the words End Function. This command will look like the following:

End Function

4.
Use If...Then to see if the intIN parameter is less than two. This line will look like:

If intIN <2 Then

5.
If the intIN parameter is less than two, then we want to assign the string "is a" the numeric value of intIN and the word Processor. The result will be that the script will use the singular form of the verb to be. This is seen in the line below:

funIS = " is a " & intIN & " Processor "

6.
If this is not the case, we will assign the string "are" and the number of processors to the name of the function. Finally we close out the If...Then construction by using End If. This is seen below:

Else
funIS = " are " & intIN & " Processors "
End If

7.
Right after we assign the colItems variable to contain the object that comes back from using the execQuery method, we want to retrieve the count of the number of items in colItems. We also want to build an output string that prints out a string stating how many processors are on the machine. We will use the funIS function to build up the remainder of the output line. It will look like the following:

WScript.Echo "There" & funIS(colItems.count) & _
"on this computer"

8.
Save and run the script. You may want to compare your results with the QueryAllProcessorsSyntax.vbs script in the folder \My Documents\Microsoft Press\VBScriptSBS\ch03.


Previous Page
Next Page