Microsoft WSH and VBScript Programming for the Absolute Beginner Part 11 - Pdf 16

80
‘Instantiate the VBScript FileSystemObject
Set FsoObject = WScript.CreateObject(“Scripting.FileSystemObject”)
‘Use the FileSystem Object object’s GetDrive method to set up a reference
‘to the computer’s C: drive
Set DiskDrive = FsoObject.GetDrive(FsoObject.GetDriveName(“c:”))
‘Main Processing Section
‘Use the FileSystemObject FreeSpace property to determine the amount of
‘free space (in MB) on the C: drive
AvailSpace = (DiskDrive.FreeSpace / 1024) / 1024
‘Use the VBScript FormatNumber Function to format the results as a
‘whole number
AvailSpace = FormatNumber(AvailSpace, 0)
‘Display the amount of free space on the C: drive
WScript.Echo “You need 100 MB of free space to play this game. “ & _
vbCrLf & “Total amount of free space is currently: “ & AvailSpace & “ MB”
The script begins by instantiating the FileSystemObject as shown here:
Set FsoObject = WScript.CreateObject(“Scripting.FileSystemObject”)
The script then uses this instance of the FileSystemObject to execute its GetDrive() method
and set up a reference to the computer’s C: drive:
Set DiskDrive = FsoObject.GetDrive(FsoObject.GetDriveName(“c:”))
The next statement uses the FileSystemObject object’s FreeSpace property to retrieve the
amount of free space on the C: drive:
AvailSpace = (DiskDrive.FreeSpace / 1024) / 1024
This statement divides this value by 1,024, and then again by 1,024, to present the amount
of free space in megabytes.
The next statement formats this value further by eliminating any numbers to the left of the
decimal point. Finally, the last statement displays the final result, as shown in Figure 3.8.
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
For more information on how to use the VBScript FileSystemObject, see Chapter 5, “Condi-
tional Logic,” in which I’ll show you how to create and write to Windows files to produce

Object to access
information about
disk drives.
82
Dim UserInput, Counter, X
UserInput = InputBox (“Type a number”, “Square Root Calculator”)
X = 1
For Counter = 1 To 15
X = X - ((X^2 - UserInput) / (2 * X))
Next
MsgBox “The square root of “ & UserInput & “ is “ & X
As you can see, the first part of the script displays a pop-up dialog to collect the number, and
the last part displays the script’s final results. The middle is where the real work results.
X = 1
For Counter = 1 To 15
X = X - ((X^2 - UserInput) / (2 * X))
Next
I won’t go into the mathematical logic behind these statements. Unless you’re a math
major, it’s a bit of a challenge to understand. This solution is based on Sir Isaac Newton’s
solution for solving square root equations. Granted, it only took four lines of code to repro-
duce the formula, but would you like to have tried to write these four statements from
scratch? I don’t think so.
Demo: A New and Improved Square Root Calculator
Now let’s look at a rewrite of the square root calculator script in which I use VBScript’s built-in
Str() function to perform square root calculations.
‘*************************************************************************
‘Script Name: SquareRoot-2.vbs
‘Author: Jerry Ford
‘Created: 11/22/02
‘Description: This script demonstrates how to solve square root

Displays text messages in the pop-up dialog boxes, giving you control over
the icons and buttons that are displayed, and, optionally, returning a value repre-
senting the button that is pressed.
83
Chapter 3 • VBScript Basics
Figure 3.9
First, the script
prompts the user
to supply a
number.
Figure 3.10
The script then
determines the
number’s
square root.
84
In addition to these two WSH options, VBScript gives you two functions of its own:

InputBox()
.
Displays a text entry field in a pop-up dialog to collect user input.

MsgBox()
.
Displays text messages in the pop-up dialog boxes, giving you control over
the icons and buttons that are displayed, and, optionally, returning a value repre-
senting the button that is pressed.
The WScript’s Echo() Method
The WScript object’s Echo() method can display text output in the Windows Command
Console or in a pop-up dialog, depending on the execution hosts that processes it. Table

3 Displays the Yes, No, and Cancel buttons
4 Displays the Yes and No buttons
5 Displays the Retry and Cancel buttons
TABLE 3.8 POPUP() METHOD B UTTON T YPES
The syntax of the second option is as follows:
WScript.Popup StrText,[Time],[TitleBarMsg],[DialogSettings]
Response
is a variable that stores a number representing the button that was clicked by the
user.
StrText represents the message text to be displayed. Time is a value that determines
how long, in seconds, the pop-up dialog will be displayed; if omitted, the default is forever.
TitleBarMsg is an optional message that is displayed in the pop-up dialog’s title bar. Finally,
DialogSettings is an option numeric value that specifies the buttons and the icon that are
to appear on the pop-up dialog. If omitted, the pop-up dialog box displays the OK button
without an icon.
To determine what numeric value to specify as the
DialogSettings value, you’ll need to
reference Tables 3.8 and 3.9. Table 3.8 lists the different collections of buttons that can be
displayed on pop-up dialogs created by the
Popup() method, and Table 3.9 displays the dif-
ferent icons that you can display using the
Popup() method.
Value Icon
16 Displays the stop icon
32 Displays the question icon
48 Displays the exclamation mark icon
64 Displays the information icon
TABLE 3.9 POPUP() METHOD I CON T YPES
86
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition

1 OK button
2 Cancel button
3 Abort button
4 Retry button
5 Ignore button
6 Yes button
7 No button
TABLE 3.10 POPUP() METHOD R ETURN VALUES
87
Chapter 3 • VBScript Basics
Response
is a variable that stores a number representing the input typed by the user. StrText
is the message that you want to display. TitlebarMsg is an optional message that will be
displayed in the pop-up dialog’s title bar.
Default is an optional default answer that you can
display in the pop-up dialog.
Xpos and ypos are optional arguments that specify, in twips, the
horizontal and vertical location of the pop-up dialog on the screen.
Helpfile and context are
also optional. They specify the location of an optional context-sensitive help file.
The following statement provides another example of
how to use the VBScript
InputBox() function:
PlayerName = InputBox(“Please type your name”)
MsgBox “You typed: “ & PlayerName
The VBScript MsgBox() Function
The VBScript MsgBox() function displays a pop-up dialog that is very similar to the pop-up
dialog produced by the WSH
Popup() method. It gives you the ability to customize the
appearance of the dialog by selecting the buttons and the icon to be displayed. You also can

MsgBox “Thanks for playing!”
You also can use the MsgBox() like this:
UserSelected = MsgBox(Would you like to play a game?)
The advantage to this last option is that you can interrogate the button that the user clicks
on and use it to drive the execution flow of your script like this:
UserSelected = MsgBox(“Would you like to play a game?”, 4, “Text Script”)
If UserSelected = 6 Then
MsgBox “OK, The rules of this game are as follows:!”
End If
Alternatively, you could rewrite the previous statements as follows:
UserSelected = MsgBox(“Would you like to play a game?”, 4, “Text Script”)
If UserSelected = vbYes Then
MsgBox “OK, let’s play!”
End If
Table 3.13 defines the list of return values associated with the various MsgBox() buttons.
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
Constant Value Description
vbCritical 16 Displays the critical icon
vbQuestion 32 Displays the question icon
vbExclamation 48 Displays the exclamation mark icon
vbInformation 64 Displays the information icon
TABLE 3.12 VBSCRIPT MSGB OX() FUNCTION I CONS
Back to the Math Game
The Math Game is played by displaying a math equation and asking the player to provide
the solution. If the player provides the correct answer, the game ends; however, if the player
gets the answer wrong, then the script offers to show the player how to arrive at the correct
answer. This is achieved in a slide slow or movie-like fashion, in which the script first starts
WordPad, then starts the Calculator application, and finally uses the applications to solve
the equation while the player sits back and watches. When the script is done with its pre-
sentation, it ends by closing both the WordPad and the Calculator applications.


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status