FSO Series II * * Design FileSystemObject

Source: Internet
Author: User
filesystemobject|fso| design to be programmed with the FileSystemObject (FSO) object mode:

Use the CreateObject method to create the FileSystemObject object.
Use the appropriate method on the newly created object.
Access the properties of the object.
The FSO object pattern is contained in the Scripting type library, which is located in the Scrrun.dll file. Therefore, to use the FSO object pattern, Scrrun.dll must be placed in the appropriate system directory of the WEB server.

Creating FileSystemObject Objects
First, use the CreateObject object to create the FileSystemObject object, and in VBScript, use the following code to create an instance of the FileSystemObject:

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

In JScript, use the following code to do the same thing:

var fso;
FSO = new ActiveXObject ("Scripting.FileSystemObject");

In both 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 a FileSystemObject object, regardless of the number of times you attempt to create another instance.

Use the appropriate method
Second, the appropriate method for using the FileSystemObject object. For example, to create a new object, use CreateTextFile or CreateFolder (the FSO object mode does not support drive creation or deletion).
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 use the appropriate method to copy and move files and folders.

------------------------------------------------------------
Note that some of the features in the FileSystemObject object pattern are superfluous. For example, you can use the CopyFile method of a FileSystemObject object, or you can copy a file by using the Copy method of the file object. The two methods are the same, and both of them can make programming flexible.

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

Accessing 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 an existing file in VBScript:

Dim FSO, F1
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f1 = fso. GetFile ("C:\test.txt")

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

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

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 by using the CreateFolder method, do not use the GetFolder method to access its properties, such as Name, Path, size, and so on. Simply 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 ("Scripting.FileSystemObject")
Set Fldr = fso. CreateFolder ("C:\MyTest")
Response.Write "Created folder:" & Fldr. Name
End Sub

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

function CreateFolder ()
{
var fso, Fldr;
FSO = ActiveXObject ("Scripting.FileSystemObject");
Fldr = fso. CreateFolder ("C:\\mytest");
Response.Write ("Created folder:" + Fldr.) Name);
}

Accessing the properties of an object
Once you have a handle to the object, you can access its properties. For example, to get the name of a particular folder, first create an instance of the object and then get its handle in the appropriate way (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 = fso. GetFolder ("C:\")

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


var Fldr = fso. GetFolder ("c:\\");

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

Response.Write "folder name is:" & Fldr. Name

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

Response.Write ("folder name is:" + Fldr.) Name);

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

Dim FSO, F1
Set fso = CreateObject ("Scripting.FileSystemObject")
' Get the file object you want to query.
Set f1 = fso. GetFile ("C:\detlog.txt")
' Print the information.
Response.Write "File Last Modified:" & F1. DateLastModified

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

var fso, F1;
FSO = new ActiveXObject ("Scripting.FileSystemObject");
Gets the file object to query.
F1 = fso. GetFile ("C:\\detlog.txt");
Print the information.
Response.Write ("File Last modified:" + F1.) DateLastModified);



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.