SoFunction
Updated on 2025-04-14

Design FileSystemObject

To program with FileSystemObject (FSO) object mode:

1Use the CreateObject method to create a FileSystemObject object.
2Use the appropriate method on the newly created object.
3Access the object's properties.
The FSO object pattern is included in the Scripting type library, which is located in the file. Therefore, to use FSO object mode, you must place it in the appropriate system directory of the web server.

Create FileSystemObject object
First, use the CreateObject object to create the FileSystemObject object. In VBScript, use the following code to create an instance of FileSystemObject:

Dim fso
Set fso = CreateObject("")

The sample code demonstrates how to create an instance of FileSystemObject.
In JScript, use the following code to do the same thing:


var fso;
fso = new ActiveXObject("");

In both examples, Scripting is the name of the type library, and FileSystemObject is the name of the object you want to create. You can create only one instance of the FileSystemObject object regardless of the number of attempts to create another instance.

Use the appropriate method
Second, use the appropriate method of the FileSystemObject object. For example, to create a new object, use CreateTextFile or CreateFolder (FSO object mode does not support creation or deletion of drives).
To delete an object, use the DeleteFile and DeleteFolder methods of the FileSystemObject object, or the Delete methods of the File and Folder objects. You can also use appropriate methods to copy and move files and folders.


--------------------------------------------------------------------------------

Note Some features in FileSystemObject Object pattern are redundant. For example, you can use the CopyFile method of the FileSystemObject object, or you can use the Copy method of the File object to copy files. Both methods have the same function; both methods can make programming flexible.

--------------------------------------------------------------------------------

Access existing drives, files, and folders
To access an existing drive, file, or folder, use the appropriate "get" method in the FileSystemObject object:

GetDrive
GetFolder
GetFile
To access existing files in VBScript:

Dim fso, f1
Set fso = CreateObject("")
Set f1 = ("c:\")

To do the same thing in JScript, use the following code:

var fso, f1;
fso = new ActiveXObject("");
f1 = ("c:\\");

Do not use the "get" method on newly created objects, because the "create" function has returned a handle to that object. For example, if you create a new folder using the CreateFolder method, do not use the GetFolder method to access its properties, such as Name, Path, Size, etc. Just set a variable to the CreateFolder function to get the handle name of the newly created folder, and then access its properties, methods, and events. To do this in VBScript, use the following code:

Sub CreateFolder
Dim fso, fldr
Set fso = CreateObject("")
Set fldr = ("C:\MyTest")
"Created folder: " &
End Sub

To set a variable to the CreateFolder function in JScript, use the following syntax:

function CreateFolder()
{
var fso, fldr;
fso = new ActiveXObject("");
fldr = ("C:\\MyTest");
("Created folder: " + );
}

Access the properties of the object
Once you have a handle to the object, you can access its properties. For example, to get the name of a specific folder, first create an instance of the object, and then get its handle with the appropriate method (in this case the GetFolder method, because the folder already exists).
In VBScript, use this code to get a handle to the GetFolder method:


Set fldr = ("c:\")

To do the same thing in JScript, use the following code:


var fldr = ("c:\\");

Now that you have the handle to the Folder object, you can check its Name property. Use the following code in VBScript to check:

"Folder name is: " &

To check the Name property in JScript, use the following syntax:

("Folder name is: " + );

To find out when the file was last modified, use the following VBScript syntax:

Dim fso, f1
Set fso = CreateObject("")
' Get the file object to query.
Set f1 = ("c:\")
' Print the information.
"File last modified: " &

To find out the same thing in JScript, use the following code:

var fso, f1;
fso = new ActiveXObject("");
// Get the file object to query.
f1 = ("c:\\");
// Print information.
("File last modified: " + );