Previous Page
Next Page

Modifying CPUType.vbs Step-by-Step Exercises

In this section, you will modify CPUType.vbs so that it uses a Select Case format instead of multiple If...Then...ElseIf statements. This is a valuable skill, because many of the scripts you will find have a tendency to use multiple If...Then...ElseIf statements. As you will see, it is relatively easy to make the modification to using Select Case. The key to success is to remove as little of the original code as possible.

1.
Open CPUTypeStarter.vbs and save it as YourNameCPUType.vbs. It is located in the \My Documents\Microsoft Press\VBScriptSBS\Ch03\StepByStep folder.

2.
Turn off On Error Resume Next by commenting out the line.

3.
Turn the If...Then line into a Select Case statement. The only element you must keep from this line is objProcessor.Architecture, because it is hard to type. When you are finished, your Select Case line looks like the following:

Select Case objProcessor.Architecture

4.
Start your case evaluation. If objProcessor.Architecture = 0, you know that the processor is an x86. So your first case is Case 0. That is all you put on the next line. It looks like this:

Case 0

5.
Leave the WScript.Echo line alone.

6.
ElseIf objProcessor.Architecture = 1 becomes Case 1, which is a MIPS CPU. Delete the entire ElseIf line and enter Case 1.

7.
Leave the WScript.Echo line alone.

ElseIf objProcessor.Architecture = 2 becomes simply Case 2, as you can see here:

Case 2

Up to this point, your Select Case configuration looks like the following:

Select Case objProcessor.Architecture
Case 0
  WScript.Echo "This is an x86 cpu."
Case 1
  WScript.Echo "This is a MIPS cpu."
Case 2
  WScript.Echo "This is an Alpha cpu."

8.
Modify the "ElseIf objProcessor.Architecture = 3 Then" line so that it becomes Case 3.

9.
Leave the WScript.Echo line alone.

The next case is not Case 4, but rather Case 6, because you modify the following line: "ElseIf objProcessor.Architecture = 6 Then". The Select Case construction now looks like the following:

Select Case objProcessor.Architecture
  Case 0
    WScript.Echo "This is an x86 cpu."
  Case 1
    WScript.Echo "This is a MIPS cpu."
  Case 2
    WScript.Echo "This is an Alpha cpu."
  Case 3
    WScript.Echo "This is a PowerPC cpu."
  Case 6
    WScript.Echo "This is an ia64 cpu."

10.
You have one more case to evaluate, and it will take the place of the Else command, which encompasses everything else that has not yet been listed. You implement Case Else by changing the Else to Case Else.

11.
Leave the line WScript.Echo "Cannot determine cpu type" alone.

12.
Change End If to End Select. Now you're finished with the conversion of If...Then...ElseIf to Select Case.

13.
Save the file and run the script. If you need assistance, refer to the CPUTypeSolution.vbs script in the same folder you found the starter script.


Previous Page
Next Page