Export (0) Print
Expand All
8 out of 10 rated this helpful - Rate this topic

The Select Case Statement

By The Microsoft Scripting Guys

Sesame Script

Welcome to Sesame Script, the column for beginning script writers. The goal of this column is to teach the very basics of Windows scripting for system administration automation. We’ll provide you with the information you’ll need to begin reading and understanding scripts and to start modifying those scripts to suit your own needs. If there’s anything in particular about scripting you’re finding confusing, let us know; you’re probably not alone.

Check the Sesame Script Archives to see past articles.

On This Page

It’s All About Teamwork
Select Case
Dinner-Time Options
A Little More Complicated
Real Teamwork
The Moral of the Story

It’s All About Teamwork

In this article we’re going to talk about teamwork. The reason we’re going to talk about teamwork is because, well, let’s just say it’s on our minds these days. And what do the Scripting Guys know about teamwork? As it turns out, very little - unless by “teamwork” you mean that one member of the team storms off and says “fine, do it yourself” anytime anyone disagrees with him.

But today the Scripting Guy who doesn’t storm off will explain teamwork.

Teamwork is where multiple individuals - meaning more than one - work together towards a common goal. Of course there are times when each individual sees the goal differently, or sees the steps to achieve that goal differently. In these cases a real team will talk it out, work together, not say things like “you’re so stupid,” and come up with some common ground. It’s really a very nice process when implemented correctly (and maturely, we might add).

Let’s take the team of Select and Case. Two very different words that seem to have nothing in common, but when they work together they turn out to be one of the most useful pairs of words in VBScript.

Select Case

Note: If you’re not reading this in English, the next paragraph will most likely make no sense at all. Skip it and go to the next paragraph, you won’t miss much.

The word select has a couple of different meanings. You can select an option; you can have a team of select players. The word case really goes crazy: a case of beer; in case of emergency; a court case; a head case. In the world of VBScript, these two words are used in the sense of “select an option” and “in case of emergency.” Not that there are any emergencies, mind you, it’s more the fact that if there is an “emergency” there are specific actions that you should take. That’s what we’re talking about.

Not clear yet? All right, we’ll just explain what the Select and Case keywords do. Alone they’re pretty useless, but use them together and they become a Select Case statement. (Are the Scripting Guys useless alone? Well, no, sometimes they work better that way - usually when one is being especially grouchy. Fortunately keywords don’t get grouchy or else you’d have some pretty frightening scripts.)

The Select Case statement is a decision statement, much like an If statement. But where an If statement takes an action based on a value being True or False, a Select Case statement takes action based on the actual value (not just True or False). For example, let’s say you have three options for dinner:

1- eat out

2 - order delivery

3 - cook

You present these options to your family and each member shouts out the number of their choice. Everyone who shouts “one” wants to eat out, everyone who shouts “two” wants to call for delivery, and everyone who shouts “three” wants to stay home and cook. Let’s see how this works in a script.

Dinner-Time Options

Here’s a very simple script that shows how to use the Select Case statement to determine which option we want for dinner:



Choice = 1

Select Case Choice
    Case 1
        Wscript.Echo "Eat Out"
    Case 2
        Wscript.Echo "Order Delivery"
    Case 3
        Wscript.Echo "Cook"
    Case Else
        Wscript.Echo "Don't Eat"
End Select

We start by defining a variable, Choice, and assigning it the value 1. The Choice variable represents the dinner option we’ve chosen.

We then start our Select Case statement. The very first part of our statement uses Select Case followed by the item (the “case”) that we’re evaluating. Here we’re evaluating the value of the Choice variable, so our statement begins like this:



Select Case Choice

In this script we want to echo back the choice that was made, so, depending on the value of the Choice variable, we need to echo the appropriate information. That’s what all the individual Case statements are for:



Case 1
    Wscript.Echo "Eat Out"
Case 2
    Wscript.Echo "Order Delivery"
Case 3
    Wscript.Echo "Cook"
Case Else
    Wscript.Echo "Don't Eat"

The first Case statement, Case 1, tells our script this: if the value of Choice is 1 we want to run all of the lines of code between Case 1 and the next Case statement (in this case Case 2). For this example, if the Choice is 1 we echo back “Eat Out”. Similarly, if Choice had a value of 2 we’d run all the lines of code between Case 2 and Case 3, which is the statement that echoes “Order Delivery”. You can probably figure out what happens if the value of Choice is 3 - that’s right, we echo “Cook”. What if the value of Choice is anything except 1, 2, or 3? In that case we run the lines of code that follow the Case Else statement, which in this example is to echo “Don’t Eat”.

Our script ends with this statement:



End Select

This statement simply closes out our Select Case statement.

There are a number of things to take note of here. The first is that we didn’t have to put our case statement in numerical order; our script would work just the same like this:



Choice = 1

Select Case Choice
    Case 2
        Wscript.Echo "Order Delivery"
    Case 3
        Wscript.Echo "Cook"
    Case 1
        Wscript.Echo "Eat Out"
    Case Else
        Wscript.Echo "Don't Eat"
End Select

The only exception is that Case Else must be the last statement. And that’s another thing: you don’t actually have to have a Case Else statement. If we were to leave the Case Else statement out (which includes the Wscript.Echo that follows it) and Choice were equal to anything other than 1, 2, or 3, the Select Case statement would simply end without echoing anything.

In addition, we aren’t limited to numbers when using Select Case. The way the Select Case statement works is that it compares the value following Select Case with the value in each individual Case statement. That means any values you can compare with an equals statement you can compare with a Select Case statement. Here’s an example that uses strings:



Choice = "Eat Out"

Select Case Choice
    Case "Eat Out"
        Wscript.Echo "Eat Out"
    Case "Order"
        Wscript.Echo "Order Delivery"
    Case "Cook"
        Wscript.Echo "Cook"
    Case Else
        Wscript.Echo "Don't Eat"
End Select

A Little More Complicated

The previous script works fine as long as you have only one choice (something certain people think should always be the case, as long as it’s their choice) and you want to go in and change the script each time you change your mind (something certain people would never do). But let’s give our user a chance to change their mind. For this we’re going to read in a value from the command line. We haven’t done this yet in Sesame Script, but there is a good Tales from the Script article all about command-line arguments.



Choice = Wscript.Arguments.Item(0)

Select Case Choice
    Case 1
        Wscript.Echo "Eat Out"
    Case 2
        Wscript.Echo "Order Delivery"
    Case 3
        Wscript.Echo "Cook"
    Case Else
        Wscript.Echo "Don't Eat"
End Select

Let’s assume we’ve named this script dinner.vbs. When we run this script we need to run it under Cscript using a command-line argument that represents our dinner choice. For example, if we want to order delivery we would run our script like this:



C:\> cscript dinner.vbs 2

The argument following the name of our script is automatically entered into an array: Wscript.Arguments.Item. Arrays in VBScript begin indexing with 0, so the first argument is Wscript.Arguments.Item(0). We then assign that value to our Choice variable.

Note: For more on arrays see Thrown for a Loop.

Now that we’ve read in our choice from the command line, we go through the same Select Case statement to echo back the choice.

Real Teamwork

This is all nice, but what if we really want to work as a team and offer everyone a choice, not just that one individual? In this script we’re using the VBScript InputBox function to display an input box to the screen. Users will type in their answer and then click either OK or Cancel. A tally will be kept of all the selection made until someone clicks Cancel:



EatOut = 0
Order = 0
Cook = 0
Hungry = 0

Do
    Choice = InputBox("Enter 1 - Eat Out; 2 - Order Delivery; 3 - Cook")

    If (Choice <> vbNull) and (Choice <> "") Then
        Select Case Choice
            Case 1
                Wscript.Echo "Eat Out"
                EatOut = EatOut + 1
            Case 2
                Wscript.Echo "Order Delivery"
                Order = Order + 1
            Case 3
                Wscript.Echo "Cook"
                Cook = Cook + 1
            Case Else
                Wscript.Echo "Don't Eat"
                Hungry = Hungry + 1
        End Select
    End If

Loop Until Choice = ""

Wscript.Echo "Eat out: " & EatOut
Wscript.Echo "Order: " & Order
Wscript.Echo "Cook: " & Cook
Wscript.Echo "Don't Eat: " & Hungry

Yes, this is starting to look a little complicated, especially since there are several things we haven’t yet talked about in Sesame Script yet. But don’t worry, it’s not nearly as complicated as it might first look.

For instance, the first four lines simply assign 0s to a bunch of variables. That’s easy enough, right? We’re just initializing the counters that will tell us how many people chose each option.

The next thing we see is one tiny little word: Do. In order to figure out what Do does, we have to look down in the script and find its partner, the Loop statement. (Look at that, more teamwork. Isn’t VBScript amazing?) We want to keep asking for options until everyone has had a chance to make their choice, so we’re going to loop around, meaning we want to do everything in between the Do and Loop statements, until no choice has been made. (How do we know when no choice has been made? When someone clicks Cancel.) We tell the Do loop to end with the Until part of the statement:



Loop Until Choice = ""

Once we’re inside our loop, we present the user with our input box using this line of code:



Choice = InputBox("Enter 1 - Eat Out; 2 - Order Delivery; 3 - Cook")

All we’re doing here is calling the InputBox function, passing it the string we want to display inside the input box. Whatever the user enters in the input box will be assigned to the Choice variable.

Next we have an If statement:



If (Choice <> vbNull) and (Choice <> "") Then

We did this because, if the user simply clicks OK or Cancel without entering anything, the Select Case statement doesn’t like that very much. Besides, we don’t want to do anything if no choice was entered. So if Choice contains a Null value (represented by the vbNull keyword) or if Choice is empty we skip the Select Case statement.

Next we have our Select Case statement. We’ve seen this before, but now we’ve added one small twist. This time, instead of simply echoing back the choice, we also increment the counter for that particular choice. As you can see, you can put multiple lines of code within a Case statement. For example, if we input 1 into the input box this part of the Select Case statement runs:



Case 1
                Wscript.Echo "Eat Out"
                EatOut = EatOut + 1

In this case we echo “Eat Out” and add 1 to the EatOut counter.

Once we exit our loop we echo back the values of all the counters:



Wscript.Echo "Eat out: " & EatOut
Wscript.Echo "Order: " & Order
Wscript.Echo "Cook: " & Cook
Wscript.Echo "Don't Eat: " & Hungry

We now know how many people want which option and we can go with whatever the majority has decided.

And what if there’s a tie? Whatever you do don’t ask the Scripting Guy who says “my way or the highway.” In that case, everyone - except him - will starve.

The Moral of the Story

See how easy that was? The Select keyword didn’t threaten to leave Case to figure it all out alone next time; Case didn’t insist the whole idea belonged to him so he got to dictate everything…they worked together nicely and produced some useful output. Wouldn’t it be nice if everyone was like Select and Case?


Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.