Flylib.com

Books Software

 
 
 

Using VBScript Run-Time Objects in Your Scripts


Using VBScript Run-Time Objects in Your Scripts

Now seems like a good time to look at an example of how to incorporate the VBScript FileSystemObject into your scripts and use its properties and methods to work with the Windows file system. Take a look at the following script:

'************************************************************************* 'Script

Name

: FreeSpace.vbs 'Author: Jerry Ford 'Created: 11/22/02 'Description: This script

demonstrates

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 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 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 numbers to the left of the decimal point. Finally, the last statement displays the final result, as shown Figure 3.8.

click to expand
Figure 3.8: Using the FileSystem Object to access information about disk drives

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 reports and log files, and Chapter 8, in which I'll cover the steps involved in opening and reading from Windows files.



Examining Built-In VBScript Functions

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."

Demo: The Square Root Calculator

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 illustrate this is by showing you two examples. In the first example, I've written a small VBScript that prompts the user to type in a number so that the script can calculate its square root. The second script is a rewrite of the first script, using the VBScript Sqr() function in place of the original programming logic.

Here's the first example.

'************************************************************************* 'Script

Name

: SquareRoot-1.vbs 'Author: Jerry Ford 'Created: 11/22/02 'Description: This script

demonstrates

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.

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 '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 mathematician in order to write the script. All you have to know is the correct way to use the Sqr() function, which is simply to pass it a number—in the case of this script, that number is represented by a variable named UserInput . These two examples show clearly the advantage of using VBScript's built-in functions. These functions can save you a lot of time and effort and perhaps a few headaches .

Figures 3.9 and 3.10 demonstrate the operation of either version of these two scripts.

click to expand
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.