Programming in the FSO Object Model

See Also

Programming in the FSO object model involves three main tasks:

The FSO object model is contained in a type library called Scripting, which is located in the file Scrrun.Dll. If you don't already have a reference to it, check "Microsoft Scripting Runtime" in the References dialog available from the Properties menu. You can then use the Object Browser to view its objects, collections, properties, methods, and events, as well as its constants.

Creating a FileSystemObject Object

The first step is to create a FileSystemObject object to work with. You can do this in two ways:

Note   The first method works only in Visual Basic, while the second method works either in Visual Basic or VBScript.

Using the Appropriate Method

The next step is to use the appropriate method of the FileSystemObject object. For example, if you want to create a new object, you can use either CreateFolder or CreateTextFile. (The FSO object model doesn't support the creation or deletion of drives.)

If you want to delete objects, you can use the DeleteFile and DeleteFolder methods of the FileSystemObject object, or the Delete method of the File and Folder objects.

Using the appropriate methods, you can also copy and move files and folders.

Note that some functionality in the FileSystemObject object model is redundant. For example, you can copy a file using either the CopyFile method of the FileSystemObject object, or you can use the Copy method of the File object. The methods work the same. Both exist to give you maximum programming flexibility.

Accessing Existing Drives, Files, and Folders

To gain access to an existing drive, file, or folder, use the appropriate "get" method of the FileSystemObject object:

For example:

Dim fso As New FileSystemObject, fil As File
Set fil = fso.GetFile("c:\test.txt")

Note, however, that you don't need to use the "get" methods for newly-created objects, since the "create" functions already return a handle to the newly-created object. For example, if you create a new folder using the CreateFolder method, you don't then need to use the GetFolder method to access its properties, such as Name, Path, Size, and so forth. Just set a variable to the CreateFolder function to gain a handle to the newly-created folder, then access its properties, methods, and events:

Private Sub Create_Folder()
   Dim fso As New FileSystemObject, fldr As Folder
   Set fldr = fso.CreateFolder("C:\MyTest")
   MsgBox "Created folder: " & fldr.Name
End Sub

Accessing the Object's Properties

Once you have a handle to an object, you can access its properties. For example, say you want to obtain the name of a particular folder. First you create an instance of the object, then you get a handle to it with the appropriate method (in this case, the GetFolder method, since the folder already exists):

Set fldr = fso.GetFolder("c:\")

Now that you have a handle to a Folder object, you can check its Name property:

Debug.Print "Folder name is: "; fldr.Name

If you want to find out the last time a file was modified, use the following syntax:

Dim fso As New FileSystemObject, fil As File
Set fil = fso.GetFile("c:\detlog.txt")  ' Get a File object to query.
Debug.Print "File last modified: "; fil.DateLastModified ' Print info.