FileSystemObject object
FileSystemObject(FSO) the object mode contains the following objects and sets.
| Object/Set |
Description |
| FileSystemObject |
Primary object. Contains methods and attributes used to create, delete, and obtain information, and to operate on drives, folders, and files. Many methods associated with this object are similar to methods in other FSO objects. They are provided for convenience. |
| Drive |
Object. Contains methods and attributes used to collect information about the drive connected to the system, such as the shared name of the drive and the available space. Note that "Drive" does not have to be a hard disk, a CD-ROM drive, a RAM disk, and so on. It is not necessary to physically Connect the drive to the system; it can also be logically connected through the network. |
| Drives |
Set. Provides a list of drives that are physically or logically connected to the system.DrivesThe set contains all the drives, regardless of the type. The media drive to be moved appears in this collection, so you do not have to insert the media into the drive. |
| File |
Object. Contains methods and attributes used to create, delete, or move files. It is also used to ask the system for file names, paths, and multiple other attributes. |
| Files |
Set. Provides a list of all files contained in the folder. |
| Folder |
Object. Contains methods and attributes used to create, delete, or move folders. It is also used to ask the system about the folder name, path, and multiple other attributes. |
| Folders |
Set. Provided inFolderList of all folders in. |
| Textstream |
Object. Used to read and write text files. |
Create an object: var FSO = new activexobject ("scripting. FileSystemObject ");
Object method: create a file createtextfile ("D: // text.txt ")
One type: F1 = FSO. createtextfile ("C: // testfile.txt", true );
Two types: TS = FSO. opentextfile ("C: // test.txt", forwriting, true );
Three types: FSO. createtextfile ("C: // test1.txt ");
F1 = FSO. GetFile ("C: // test1.txt ");
TS = f1.openastextstream (forwriting, true );
Create the createfolder directory ('d: // DDD ')
Other methods: getdrive, getfolder, GetFile
AvailableDriveObjects to obtain information about various drives that are physically or over a network connected to the system. Its attributes can be used to obtain the following information:
- Total capacity of the drive, in bytes (TotalsizeAttribute)
- Disk space, in bytes (AvailablespaceOrFreespaceAttribute)
- Which is assigned to the drive (DriveletterAttribute)
- What is the drive type, such as removable, fixed, network, CD-ROM, or RAM disk (DrivetypeAttribute)
- Serial number of the drive (serialnumber attribute)
- The type of the file system used by the drive, such as fat, FAT32, NTFS, etc (FilesystemAttribute)
- Whether the drive can be used (IsreadyAttribute)
- Name of the shared and/or volume (sharename and volumename attributes)
- Path or root folder of the drive (PathAndRootfolderAttribute)
- Function showdriveinfo1 (drvpath)
{
var fso, drv, s ="";
fso = new ActiveXObject("Scripting.FileSystemObject");
drv = fso.GetDrive(fso.GetDriveName(drvPath));
s += "Drive " + drvPath.toUpperCase()+ " - ";
s += drv.VolumeName + "<br>";
s += "Total Space: " + drv.TotalSize / 1024;
s += " Kb" + "<br>";
s += "Free Space: " + drv.FreeSpace / 1024;
s += " Kb" + "<br>";
Response.Write(s);
}
Process folders
The following table describes common Folder Tasks and how to execute them.
| Task |
Method |
| Create a folder. |
FileSystemObject. createfolder |
| Delete a folder. |
Folder. delete or FileSystemObject. deletefolder |
| Move the folder. |
Folder. Move or FileSystemObject. movefolder |
| Copy a folder. |
Folder. Copy or FileSystemObject. copyfolder |
| Search for the folder name. |
Folder. Name |
| Find the folder if it exists on the drive. |
FileSystemObject. folderexists |
| Obtain existingFolderObject instance. |
FileSystemObject. getfolder |
| Find the parent folder Name of the folder. |
FileSystemObject. getparentfoldername |
| Find the path of the system folder. |
FileSystemObject. getspecialfolder |
function ShowFolderInfo(){var fso, fldr, s = "";// Obtain FileSystemObject .fso = new ActiveXObject("Scripting.FileSystemObject");// Obtain Drive Object.fldr = fso.GetFolder("c:");// Print the parent folder name.Response.Write("Parent folder name is: " + fldr + "<br>");// Print the drive name.Response.Write("Contained on drive " + fldr.Drive + "<br>");// Print the root file name.if (fldr.IsRootFolder)Response.Write("This is the root folder.");elseResponse.Write("This folder isn't a root folder.");Response.Write("<br><br>");// Use FileSystemObject Object To create a new folder.fso.CreateFolder ("C://Bogus");Response.Write("Created folder C://Bogus" + "<br>");// Print the basic name of the folder.Response.Write("Basename = " + fso.GetBaseName("c://bogus") + "<br>");// Delete a newly created folder.fso.DeleteFolder ("C://Bogus");Response.Write("Deleted folder C://Bogus" + "<br>");}
Add data to a fileOnce a text file is created, use the following three steps to add data to the file:
Open a text file.
Write Data.
Close the file.
To open an existing file, use the opentextfile method of the FileSystemObject object or the openastextstream method of the file object.
To write data to an open text file, use the write, writeline, or writeblanklines methods of the textstream object as described in the following table.
| Task |
Method |
| Write Data to open text files without a new line character. |
Write |
| Write Data to the opened text file, followed by a new line character. |
Writeline |
| Write one or more blank lines to the opened text file. |
Writeblanklines |
To close an open file, useTextstreamObjectCloseMethod.
Note:The new line contains one or several characters (depending on the operating system) to move the cursor to the starting position of the next line (carriage return/line feed ). Note that this non-printable character may already exist at the end of some strings.
Function createfile ()
{
var fso, tf;
fso = new ActiveXObject("Scripting.FileSystemObject");
tf = fso.CreateTextFile("c://testfile.txt", true);
// Write a line with a new line character.
tf.WriteLine("Testing 1, 2, 3.") ;
// Write three new line characters to the file.
tf.WriteBlankLines(3) ;
// Write a row.
tf.Write ("This is a test.");
tf.Close();
}
Read files
To read data from a text file, useTextstreamObjectRead,ReadlineOrReadallMethod. The following table describes the methods used by different tasks.
| Task |
Method |
| Reads a specified number of characters from a file. |
Read |
| Read a whole line (until but not including new line characters ). |
Readline |
| Read the entire content of a text file. |
Readall |
If you useReadOrReadlineTo skip the special part of the data, useSkipOrSkiplineMethod. The result text of the read method is included in a string, which can be displayed in a control or used as a string function (suchLeft,RightAndMid.
Function readfiles ()
{
var fso, f1, ts, s;
var ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.CreateTextFile("c://testfile.txt", true);
// Write a row.
Response.Write("Writing file <br>");
f1.WriteLine("Hello World");
f1.WriteBlankLines(1);
f1.Close();
// Read the content of a file.
Response.Write("Reading file <br>");
ts = fso.OpenTextFile("c://testfile.txt", ForReading);
s = ts.ReadLine();
Response.Write("File contents = '" + s + "'");
ts.Close();
}
Move, copy, and delete objects
The FSO object mode has two ways to move, copy, and delete files, as described in the following table.
| Task |
Method |
| Move files |
File. Move or FileSystemObject. movefile |
| Copy a file |
File. Copy or FileSystemObject. copyfile |
| Delete an object |
File. delete or FileSystemObject. deletefile |
The following VBScript example creates a text file in the root directory of drive C, writes some information to it, and then moves it to the/tmp directory, make a backup in/temp and delete them from the two directories.
function ManipFiles(){var fso, f1, f2, s; fso = new ActiveXObject("Scripting.FileSystemObject"); f1 = fso.CreateTextFile("c://testfile.txt", true); Response.Write("Writing file <br>"); // Write a row.f1.Write("This is a test.");// Close the file.f1.Close(); Response.Write("Moving file to c://tmp <br>");// Obtain C Root directory(C:/)File handle.f2 = fso.GetFile("c://testfile.txt");// Move files /tmp Directory.f2.Move ("c://tmp//testfile.txt"); Response.Write("Copying file to c://temp <br>"); // Copy a file /temp Directory.f2.Copy ("c://temp//testfile.txt"); Response.Write("Deleting files <br>");// Obtains the handle of the current file location.f2 = fso.GetFile("c://tmp//testfile.txt"); f3 = fso.GetFile("c://temp//testfile.txt");// Delete an object.f2.Delete(); f3.Delete(); Response.Write("All done!");}