Now seems like a good time to look at an example of how to
'************************************************************************* 'ScriptName : FreeSpace.vbs 'Author: Jerry Ford 'Created: 11/22/02 'Description: This scriptdemonstrates how to use VBScript run-time 'objects and their properties and methods. '************************************************************************* 'Initialization Section Option Explicit Dim FsoObject, DiskDrive, AvailSpace '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 at least 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 , like this:
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
AvailSpace = (DiskDrive.FreeSpace / 1024) / 1024
This statement divides this value by 1024, and then again by 1024, to present the amount of free space in MB.
The next statement formats this value further by eliminating any
Figure 3.8:
Using the
FileSystem Object
to access information about disk
For more information on how to use the VBScript
FileSystemObject
, see Chapter 5, in which I'll show you how to create and write to Windows files in order to produce
One of the real advantages of working with VBScript is having access to its large number of built-in functions. In the previous example, you saw how to use the FormatNumber() function. There are too many built-in VBScript functions for me to list them all here. For a complete list, refer to Appendix B, "Built-In VBScript Functions."
By using functions, you can really streamline your scripts. VBScript' s built-in functions provide built-in code that you don't have to write. The best way that I can think to
Here's the first example.
'************************************************************************* 'ScriptName : SquareRoot-1.vbs 'Author: Jerry Ford 'Created: 11/22/02 'Description: This scriptdemonstrates how to solve square root 'calculations using a mathematic solution devised by Sir Issac Neuton '************************************************************************* 'Initialization Section Option Explicit 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 popup dialog to collect the number, and the last part displays the script's final results. It is in the middle where the real work results.
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}
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 Issac Newton's solution for solving square root equations. Granted, it only took four lines of code to reproduce the formula, but would you like to have tried to write these four statements from scratch? I don't think so.
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 'calculations using VBScript's Built-in Sqr() function '************************************************************************* 'Initialization Section Option Explicit Dim UserInput UserInput = InputBox ("Type a number", "Square Root Calculator") MsgBox "The square root of "& UserInput & "is "& Sqr(UserInput)
As you can see, this time you don't have to be a
Figures 3.9 and 3.10
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.