[Previous] [Table of Contents] [Next]

The FileSystemObject Object Model

The FileSystemObject (fso for short) object is available in both VBScript and JScript. Its subobjects, methods, and properties are listed in Table 12-1.

Table 12-1 The FileSystemObject Object Model

Object or CollectionDescription
Drives A collection of all drives (logical or physical) known on the machine, including drives with removable media (such as floppy disks)
Drive An object whose methods and properties you can use to access a drive (fixed disk, mapped network drive, CD-ROM drive, or floppy disk drive), retrieve free space on the medium, and so on
Folders A collection of all subfolders in a given folder
Folder An object whose methods you can use to create, move, rename, and delete folders and whose properties you can use to retrieve folder names, paths, and so on
Files A collection of all files in a folder
File An object whose methods you can use to create, delete, rename, and move files and whose properties you can use to retrieve filenames and paths
FileSystemObject The main object of the FileSystemObject object model, which provides all methods and properties for file system access
TextStream An object whose methods you can use to read and write text files

The FileSystemObject object (which is provided in the file Scrrun.dll) doesn't have any binary file access methods. You can read or write a binary file only by using the text file methods, with all the accompanying disadvantages: You'll need to convert all data within your script, you'll have no direct methods for handling binary data types, and so on. The VBScript and JScript language references for the version 5 script engines contain detailed, up-to-date information on the FileSystemObject object model.

Creating a FileSystemObject Object

Before you can access the objects, methods, and properties of the FileSystemObject object model, you must create an object variable and assign a reference to the FileSystemObject object. This object is part of the language itself, so you can also use the native methods of VBScript or JScript to retrieve the object reference.

As you'll recall, the syntax for creating an object reference and assigning a reference looks like this in VBScript:

Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

The second line uses the CreateObject method of WScript to load an object instance and assign a reference to it to the object variable fso.

In JScript, you can use the following line to create a reference to the FileSystemObject object:

var fso = WScript.CreateObject("Scripting.FileSystemObject");

This statement creates the object variable fso, creates an object instance, and assigns the reference to this object instance to the object variable.

Methods of the FileSystemObject Object

After you create the FileSystemObject object, you can access its methods, properties, and subobjects. To create a new folder, you use the CreateFolder method. To create a new text file, you use the CreateTextFile method—and so on. You can delete items such as files and folders by using the DeleteFile and DeleteFolder methods, respectively. FileSystemObject also has methods for copying and moving files and folders.

The FileSystemObject object duplicates some of the functionality of its subobjects so that programmers have more flexibility when writing scripts. For example, you can copy files by using the CopyFile method of the FileSystemObject object or the Copy method of the File object. The corresponding methods of the various file and folder objects use slightly different parameters, so you might have to check the language reference for the correct parameters of each method you want to use.

You use the GetDrive, GetFolder, and GetFile methods of the FileSystemObject object to access drives, folders, or files. The following VBScript statements create a reference to a file:

Dim fso, oFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.GetFile("C:\Test.txt")

Here's the JScript equivalent:

var fso = new ActiveXObject("Scripting.FileSystemObject");
var oFile = fso.GetFile("C:\\Test.txt");

Both code sequences use the GetFile method of the FileSystemObject object (which is kept in the object variable fso). The method returns a reference to the object variable oFile. You can use this oFile variable to access the file.