Handling User Input in WSH - Wscript Examples
(Page 6 of 6 )
Let’s go back to one of our Cscript examples and rewrite it for Wscript. First, we’ll ask for the user’s name. Then, we’ll ask for their age. We’ll finish up by showing our results.
strName = InputBox("What is your name?", "About You", _
"Enter your name")
result = MsgBox("You have indicated that your name is " & strName _
& vbCr & "Is this correct?", VBYesNo + VBQuestion, _
"Please check your name")
strAge = InputBox("How old are you?", "About You", "Enter your age")
result = MsgBox("Your Name is " & strName & ". You are " & strAge _
& ".",, "Your Results")
This produces a series of input and message boxes to gather the user’s name and age. It finishes by displaying the results. There are a few things to note in this example. First, notice the two different ways we’ve called the MsgBox function. I’ve used a mixture of the optional attributes.
You should also notice the last message box. I’ve used the optional Title attribute but have not used the Button attribute. I included an extra comma to show where the Button attribute would have been. Even though the Button attribute is missing, the Title attribute must still be the third attribute passed to the function.
The first message box is used check that the name is correct. It doesn’t do much good if we don’t do something to handle the result.
strName = InputBox("What is your name?", "About You", _
"Enter your name")
result = MsgBox("You have indicated that your name is " & strName _
& vbCr & "Is this correct?", VBYesNo + VBQuestion, _
"Please check your name")
If result = VBNo Then
MsgBox("Sorry. Please try again.", 16, "Program Terminated")
Wscript.Quit
End If
strAge = InputBox("How old are you?", "About You", "Enter your age")
result = MsgBox("Your Name is " & strName & ". You are " & strAge & _
".",, "Your Results")
I’ve added an If statement to check the result of the first message box. If the No button was pressed it notifies the user and ends the script. Notice that I’ve used the exact values to call this message box.
Although you can call the MsgBox function using either constants or values for the Button attribute you should maintain a sense of continuity in your script by using the same method throughout.
We should also add some code in case our user presses the Cancel button on any of our input boxes. To avoid stretching this code sample on forever I’m going to make use of a subroutine.
strName = InputBox("What is your name?", "About You", _
"Enter your name")
If strName = "" Then Call UserCancel
result = MsgBox("You have indicated that your name is " & strName _
& vbCr & "Is this correct?", VBYesNo + VBQuestion, _
"Please check your name")
If result = VBNo Then
result = MsgBox("Sorry. Please try again.", 16, _
"Program Terminated")
Wscript.Quit
End If
strAge = InputBox("How old are you?", "About You", "Enter your age")
If strAge = "" Then Call UserCancel
result = MsgBox("Your Name is " & strName & ". You are " & strAge & _
".",, "Your Results")
Sub UserCancel
result = MsgBox("You have chosen to end this program.", _
vbInformation, "Program Terminated")
Wscript.Quit
End Sub
If you’re using a message box that has multiple buttons, it may be easier to use a Select statement to check your answers.
It’s possible to use a Select statement to process message box results because the result constants actually represent the integer values.
Here’s a quick example of what that might look like. Try running it a few times and press each of the possible buttons.
result = MsgBox("Press a button", VBAbortRetryIgnore)
Select Case result
Case VBAbort
Wscript.Echo "You pressed Abort"
Case VBRetry
Wscript.Echo "You pressed Retry"
Case Else
Wscript.Echo "You pressed Ignore"
End Select
Now you’ve seen how to handle user input in both Cscript and Wscript using command line parameters when calling your script and by prompting for the information during execution. This can add a whole new dimension to your scripting practices. Thanks for reading once again. Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |