![]() |
Table of Contents |
![]() |
Asking for InputAlthough the MsgBox() function provides a way to collect simple Yes/No style input, you may need to collect more complex input, such as server names, user names, or other data. VBScript provides a way for users to input this type of string information. Graphical InputThe InputBox() function displays a graphical input box with a title, a short message, a one-line text input box, and an OK and Cancel button. Whatever the user types is returned as the result of the function; if the user clicks Cancel or presses Esc on his keyboard, the function returns -1. Figure 6.4 shows what this quick sample looks like. Dim vInput vInput = InputBox("Enter a server name","Server") MsgBox "You entered " & vInput Figure 6.4. Collecting text input by using InputBox()You should always test to see if the user clicked Cancel or pressed Esc. Dim vInput vInput = InputBox("Enter a server name","Server") If vInput = -1 Or vInput = "" Then MsgBox "You cancelled." Else MsgBox "You entered " & vInput End If This type of check prevents your script from trying, for example, to connect to a server named \\-1 when the user cancels the input box. You can expand InputBox() slightly to provide a default entry. Users can accept your default by simply clicking OK or pressing Enter when the input box is displayed, or they can type their own input instead of your default. Here's how. Dim vInput, vDefault vDefault = "\\ServerA" vInput = InputBox("Enter a server name","Server",vDefault) If vInput = -1 Then MsgBox "You cancelled." ElseIf vInput = vDefault Then MsgBox "You selected the default, ServerA." Else MsgBox "You entered " & vInput End If Your default entry simply becomes the third parameter of the InputBox() function. It will be shown in the input box and selected, allowing users to simply start typing if they want to enter their own input rather than accept your default. Command-Line InputAsking for input from a command-line script is a bit more complex. Unfortunately, there's no command-line version of the InputBox() function to make things simple. Instead, you have to deal with something called StdIn, which is the system's standard input stream, representing text typed by the user. For example: WScript.Echo "Type something, and then press Enter, to continue." Dim vInput vInput = "" Do While Not WScript.StdIn.AtEndOfLine Input = Input & WScript.StdIn.Read(1) Loop WScript.Echo "You typed " & Input This script collects one character at a time from StdIn until the user presses Enter. At that point, StdIn's AtEndOfLine property is set to True, and the loop terminates. Note that this script will only work under Cscript; WScript doesn't supply access to StdIn when you're running a graphical environment script. Figure 6.5 shows what this example looks like from the command line. If you try to run this script from within WScript, you'll receive an error message on the fourth line of code saying, "The handle is invalid." Figure 6.5. Collecting text input from the command lineIf you'd like to learn more about StdIn and command-line text input, refer to the Windows Script Host documentation at http://msdn.microsoft.com/scripting. |
![]() |
Table of Contents |
![]() |