Working with the Windows File System

[ LiB ]

Working with the Windows File System

The primary JScript runtime objects are the FileSystemObject and Dictionary objects. The Dictionary object provides the capability to set up an array whose contents can be accessed using keys instead of by index position. All other JScript runtime objects are derived from FileSystemObject .

NOTE

The Dictionary object provides all the benefits provided by arrays with better flexibility when it comes to retrieving data that is stored with an associated key (which can be used later to retrieve the item's value). Use of the Dictionary object is an advanced programming topic that is beyond the scope of this book.To learn more about the Dictionary object, check out Microsoft JScript Professional Projects , published by Premier Press.

To use FileSystemObject , you must first instantiate it. This is done as demonstrated here.

 var fsoObject = new ActiveXObject ("Scripting.FileSystemObject"); 

In this example, fsoObject is the name I assigned to the object reference for the FileSystemObject . Using this reference, you can access any of the FileSystemObject object's properties and methods , as well as instantiate other runtime objects. For example, the following JScript demonstrates how to instantiate the FileSystemObject and then use its GetDrive() method to instantiate the Drive object in order to use that object's FreeSpace property. The end result is a script that reports the total amount of free space available on the computer's C drive.

 //************************************************************ ************* //Script Name: Script 6.3.js //Author: Jerry Ford //Created: 09/12/03 //Description: This JScript shows how to use JScript runtime //objects to access information about the computer C: drive //************************************************************ *************  //Initialization Section   //Instantiate the FileSystemObject object   var fsoObject = new ActiveXObject("Scripting.FileSystemObject");   //Instantiate the Drive object   var diskDrive = fsoObject.GetDrive(fsoObject.GetDriveName("c:")); //Main Processing Section   //Call the function that displays drive information   GetDriveInfo();   //Terminate the script's execution   WScript.Quit(); //Procedure Section   //This subroutine displays the amount of drive free space   function GetDriveInfo() {     WScript.Echo("The amount of free space currently available on C: is " +       ((diskDrive.FreeSpace / 1024) / 1024) + " MB");   } 

The script begins by instantiating the FileSystemObject followed by the Drive objects as shown here.

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); var diskDrive = fsoObject.GetDrive(fsoObject.GetDriveName("c:")); 

Once the FileSystemObject object has been established, its GetDrive() method is used to create the Drive object reference. Notice that the reference returned is for the computer's C drive.

Next the script calls the GetDriveInfo() function, which is shown here.

 function GetDriveInfo() {     WScript.Echo("The amount of free space currently available on C: is " +       ((diskDrive.FreeSpace / 1024) / 1024) + " MB"); } 

This function uses the WScript object's Echo() method to display a string. This string consists of text that includes an embedded reference to the Drive object's FreeSpace property. The value returned by this property is the amount of unused disk space (in bytes) on the specified disk drive. This value is then divided by 1,024 twice in order to convert it to MB. Finally, the script terminates its own execution with the WScript.Quit() statement.

This script uses the WScript object's Echo() method to display its output. This method displays text output differently, depending on whether it is executed in a script run from the Windows command line or the Windows desktop. Figures 6.5 and 6.6 demonstrate effects of running this script from both of these locations.

Figure 6.5. The result of the script when run from the Windows command prompt

graphic/06fig05.gif


Figure 6.6. The result of the script when run from the Windows desktop

graphic/06fig06.gif


[ LiB ]


Learn JavaScript In a Weekend
Learn JavaScript In a Weekend, Second Edition
ISBN: 159200086X
EAN: 2147483647
Year: 2003
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net