Programming the FileSystemObject
To program with the FileSystemObject (FSO) object model:
Use the CreateObject method to create a FileSystemObject object.
Use the appropriate method on the newly created object.
Access the object's properties.
The FSO object model is contained in the Scripting type library, which is located in the Scrrun.dll file. Therefore, you must have Scrrun.dll in the appropriate system directory on your Web server to use the FSO object model.
First, create a FileSystemObject object by using the CreateObject method.
The following code displays how to create an instance of the FileSystemObject:
In both of these examples, Scripting is the name of the type library and FileSystemObject is the name of the object that you want to create. You can create only one instance of the FileSystemObject object, regardless of how many times you try to create another.
Second, use the appropriate method of the FileSystemObject object. For example, to create a new object, use either CreateTextFile or CreateFolder (the FSO object model doesn't support the creation or deletion of drives).
To delete objects, use the DeleteFile and DeleteFolder methods of the FileSystemObject object, or the Delete method of the File and Folder objects. You can also copy and move files and folders, by using the appropriate methods.
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 offer programming flexibility.
To gain access to an existing drive, file, or folder, use the appropriate "get" method of the FileSystemObject object:
To gain access to an existing file:
Dim fso, f1 Set fso = CreateObject("Scripting.FileSystemObject") Set f1 = fso.GetFile("c:\test.txt")
var fso, f1; fso = new ActiveXObject("Scripting.FileSystemObject"); f1 = fso.GetFile("c:\\test.txt");
Do not use the "get" methods for newly created objects, since the "create" functions already return a handle to that object. For example, if you create a new folder using the CreateFolder method, don't 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.
To set a variable to the CreateFolder function, use this syntax:
Sub CreateFolder Dim fso, fldr Set fso = CreateObject("Scripting.FileSystemObject") Set fldr = fso.CreateFolder("C:\MyTest") Response.Write "Created folder: " & fldr.Name End Sub
Once you have a handle to an object, you can access its properties. For example, to get the name of a particular folder, first create an instance of the object, then get a handle to it with the appropriate method (in this case, the GetFolder method, since the folder already exists).
Use this code to get a handle to the GetFolder method:
Now that you have a handle to a Folder object, you can check its Name property.
To find out the last time a file was modified, use the following syntax:
Dim fso, f1 Set fso = CreateObject("Scripting.FileSystemObject") ' Get a File object to query. Set f1 = fso.GetFile("c:\detlog.txt") ' Print information. Response.Write "File last modified: " & f1.DateLastModified