InputBox
One of the great advantages of VBScript over traditional scripting, such as batch files, is the
ability to solicit information or input from the user executing the script. This is typically done
with the InputBoxfunction, as shown here.
Dim objFSO, objTS
strTitle="Select Text File"
strFile=InputBox("Whatfile do you want to open?",strTitle,"C:\boot.ini")
'if value of strFile is blank then either nother was entered
'or Cancel was clicked. In either case we can't continue
If strFile="" Then WScript.Quit
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objTS=objFSO.OpenTextFile(strFile)
'script continues
The Basics of Advanced Windows Scripting
As you can see in this brief example, the script asks the user for a file name by using the InputBoxfunction. Although the only parameter required is text to explain what the user should
enter, your script will be more user-friendly if you include a title and a default choice. The
advantage of offering a default choice is that users have a better idea of exactly what format
they should use.
After you get input, you should validate it, as we did in the code just shown. If the user clicks
Cancel or doesn’t enter anything, there is no reason to continue, so the script silently quits.
Depending on the type of information you are seeking, you might want to do further validation, such as checking the length or size of the entry. Or as you can validate
the value itself.
InputBox with Menu Sample
On Error Resume Next
strTitle="Option Menu"
strMenu="Please select one of the following choices:" & VbCrLf
strMenu=strMenu & "1 - Banana Cream" & VbCrLf
strMenu=strMenu & "2 - Cherry" & VbCrLf
strMenu=strMenu & "3 - Apple Walnut" & VbCrLf
strMenu=strMenu & "4 - Peach"
rc=InputBox(strMenu,strTitle,1)
If rc="" ThenWScript.Quit
Select Case rc
Case "1"
WScript.Echo "One slice of Banana Cream, coming up!"
Case "2"
WScript.Echo "Sorry, we are all out of cherry."
Case "3"
WScript.Echo "Do youwant ice cream with that?"
Case "4"
WScript.Echo "You get the lastpiece of Peach."
Case Else
WScript.Echo Chr(34) & rc & Chr(34) &_
" is not a valid choice. Please try again."
WScript.quit
End Select
'script continues
we build a text string in the strMenuvariable. This variable is passed as the message parameter for the InputBoxfunction. Assuming the value returned by the InputBoxis not
blank, we can use Select Caseto determine the next course of action.
Even though we expect the user to enter a number, he or she might accidentally type some
non-numeric character. By enclosing the choices in quotes for the Casestatement, we treat the
value as a literal text value. In this way, we are assured that the error handling code in Case
Elsewill work. If the user enters anything other than 1, 2, 3, or 4, the error message is displayed. Entering A,which should be invalid, returns the code for Case 1.Copy the script for
Alas, the InputBoxfunction is the only graphical input option we have, other than using an
HTML Application (HTA) (which we cover later in the book) or developing your own input
box in a higher-level programming language such as Microsoft Visual Basic 2005 (which is
beyond the scope of this book).
MsgBox
Closely related to the InputBoxfunction, the MsgBox function also displays a message to the
user in a graphical dialog box. At its simplest, all you need to code is the MsgBoxfunction and
text to be displayed.
MsgBox "Welcome to the company!"
This line displays a message box in which the user must click OK to proceed. Script execution
halts until the message box is dismissed. Recall that you use the WshShell.Popupmethod to set
a time interval that determines how long to display the message. You can force a popup window to behave like a message box by setting the timeout value to –1, which requires the user
to click a button to dismiss it.
You can use a MsgBoxfunction to display information or to get information, such as whether
the user wants to continue working on the current task. The MsgBoxfunction returns a value
determined by the button clicked. You can create a message box that offers the button options
OK, Yes, No, or Cancel.
Note There are other button types available, but these are the ones you are most likely to
use in a script. See the Windows Script Host 5.6 documentation for additional information.
MsgBoxYesNo Sample
strMsg="The file already exists. Do you want to overwrite it?"
strTitle="File Confirm"
rc=MsgBox(strMsg,vbYesNo,strTitle)
If rc=vbYes Then
Script.Echo "Overwriting file"
'insert code here
Else
strNewName=InputBox("Enter a new filename.",_
strTitle,"c:\logs\newlog.txt")
'insert code here
End If
'script continues
A MsgBoxfunction asks whether the user wants to overwrite the file and uses the constant
vbYesNoto create Yes and No buttons. We set a rcvariable to return a value from the MsgBox
14 Part I: The Basics of Advanced Windows Scripting
depending on what button the user clicked.We can then add code depending on the returned
value. But what if the user has a change of heart and wants to abort the entire script? Take a
look at
MsgBoxYesNoCancel Sample
strMsg="The file alreadyexists. Do you want to overwrite it?"
strTitle="File Confirm"
rc=MsgBox(strMsg,vbYesNoCancel,strTitle)
'take next steps based onvalue returned by
'MsgBox function
Select Case rc
Case vbYes
WScript.Echo"Overwriting file"
'insert code here
Case vbNo
strNewName=InputBox("Enter a new filename.",_
strTitle,"c:\logs\newlog.txt")
'insert code here
Case vbCancel
WScript.Echo"Aborting the script"
WScript.Quit
End Select
'script continues
we use a Select Casestatement to handle the MsgBoxvalue. The code for vbYes
and vbNois unchanged. All we did was add code to handle vbCancel.
Tip You can also use vbYesNo, vbOKOnly, and vbYesNoCancelas button options in a
WshShellpopup. The value returned is an integer, depending on what button is clicked, but it
is easier to use the intrinsic constants like vbYes. If you don’t use the constants, you have to figure out what the constant equivalent is and use that in your code, and that probably won’t be
as meaningful unless you comment heavily. Use the constants and make your life easier.
There is one more feature of the MsgBoxfunction that also works for the WshShell popup—the
ability to add an icon to the dialog box. Table 1-1 shows the icons available.
To include an icon, simply add it with the appropriate button type, for example,
vbOkOnly+vbInformation. Take a look at Listing 1-11, which is the script from Listing 1-10
slightly modified to use icons.
MsgBoxIcon Constants
VBScript Constant Integer Value Icon Displayed
vbCritical 16 Critical Message
vbQuestion 32 Warning Query
vbExclamation 48 Warning Message
vbInformation 64 Information
MsgBoxwith Icon Sample
strMsg="The file already exists. Do you want to overwrite it?"
strTitle="File Confirm"
rc=MsgBox(strMsg,vbYesNoCancel+vbQuestion,strTitle)
'take next steps based on value returned by
'MsgBox Function
Select Case rc
Case vbYes
MsgBox "Overwriting file",vbOKOnly+vbInformation,strTitle
'insert code here
Case vbNo
strNewName=InputBox("Enter a new filename.",_
strTitle,"c:\logs\newlog.txt")
'insert code here
Case vbCancel
MsgBox "Aborting the script",vbOKOnly+vbCritical,strTitle
WScript.Quit
End Select
'script continues
Now our message boxes not only have a little morepizzazz, but they also provide visual reinforcement to the user. You can use these icon constants in a WshShellpopup, but unfortunately, you can’t use them with an InputBox.
No comments:
Post a Comment