Article

Make choices

Christa Anderson

You may recall from earlier columns that VBScript normally runs through a script from top to bottom until it runs out of commands. One way to change this flow is to use conditional statements that set conditions under which code should execute. If the conditions aren't met, the code doesn't run.

The simplest conditional statement is the If... Then. IF this condition is true, THEN do this. If it's Tuesday, then have lunch with Joe. A variation is If...Then...Else, which says: IF this condition is true, THEN do this, ELSE do that. If it's Tuesday, then have lunch with Joe -- otherwise, have lunch with Teresa.

If...Then statements are fine when a script must only choose from among a couple of choices, but they can get unwieldy quickly. If you've got more than a couple of potential lunch dates, you're better off with a Select Case statement.

Select Case sets the condition that you're testing (in this case, the day of the week) and then has options for each result, like this:
Select Case Day
Case Monday lunch with Sue
Case Tuesday lunch with Joe
Case Wednesday lunch with Francis

If you don't have lunch dates every day of the week, then you can take advantage of an option of Select Case: Case Else. This option says that, if it's any result other than one of the ones called out explicitly, perform this action. In this example, you'll set a variable equal to oNet.UserName and, based on the value of this

Requires Free Membership to View

variable, map the appropriate network drives to drive letters. Case Else provides a default set of mappings for any names for which you haven't explicitly defined resources.

 

Scripting: Connect users to network resources
- Introduction
- Set your goals
- Identify resources
- Make choices
- Summary

ABOUT THE AUTHOR:
When Christa Anderson began working with Windows Server operating systems in 1992, she became increasingly interested in finding more efficient and flexible ways of performing routine tasks. Christa has written extensively about administrative scripting and taught technical sessions on the subject at conferences such as Comdex and CeBIT, helping people who had never done any scripting to write their own scripts in half a day. In addition to her interest in scripting Windows management, Christa is an authority on server-based computing and the program manager for Terminal Services licensing in Longhorn. If you have a scripting question for Christa, please e-mail her at scripting@SearchWinSystems.com.


There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: