I. function implementation core: FileSystemObject object In fact, to implement file operations in Javascript, FileSystemObject objects are mainly used. Before giving a detailed description of the attributes and methods of a FileSystemObject object, let's take a look at the related objects and sets of this object: Driver object type. This includes methods and attributes for collecting information about the drive in the system, which are not as good as the shared name and available space. A drive object does not necessarily represent a physical hard disk, it can also be a CD-ROM drive, a ramdisk, or a resource that is logically connected over the network. Drivers collection type. Provides a series of drive objects that exist physically or logically in the system. File object type. This includes methods and attributes for creating, deleting, or moving files, as well as methods and attributes for querying file names, paths, and other file attributes. Files set type. Provides a series of file objects contained in the folder. Folder object type. Including methods and properties for creating, deleting, or moving folders. Folders collection type. Provides a series of folder objects contained in the folder. Textstream object type. Supports reading and writing text files. |
Ii. FileSystemObject programming Trilogy |
It is very easy to program with FileSystemObject object. Generally, the following steps are required: Create a FileSystemObject object, apply related methods, and access object attributes. |
(1) create a FileSystemObject object |
Only one line of code is required to create a FileSystemObject object: |
VaR FSO = new activexobject ("scripting. FileSystemObject "); |
After the above code is executed, FSO becomes a FileSystemObject object instance. |
(2) Application-related methods |
After creating an object instance, you can use the object-related methods. For example, use the createtextfile method to create a text file: |
VaR FSO = new activexobject ("scripting. FileSystemObject "); |
VaR F1 = FSO. createtextfile ("C: \ myjstest.txt", true "); |
(3) Access Object Attributes |
To access the relevant properties of an object, you must first create a handle pointing to the object, which must be implemented through the get series method: getdrive is responsible for obtaining the drive information, getfolder is responsible for obtaining the folder information, getFile obtains the file information. For example, after pointing to the following code, F1 becomes the handle pointing to the file c: \ test.txt: |
VaR FSO = new activexobject ("scripting. FileSystemObject "); |
VaR F1 = FSO. GetFile ("C :\\ myjstest.txt "); |
Then, use F1 to access the relevant attributes of the object. For example: |
VaR FSO = new activexobject ("scripting. FileSystemObject "); |
VaR F1 = FSO. GetFile ("C :\\ myjstest.txt "); |
Alert ("file last modified:" + f1.datelastmodified ); |
After the last sentence is executed, the Last modified Date attribute value of C: \ myjstest.txt is displayed. |
Note: For objects created using the create method, you do not need to use the get method to obtain the object handle. In this case, you can directly use the name of the handle created using the create method: |
VaR FSO = new activexobject ("scripting. FileSystemObject "); |
VaR F1 = FSO. createtextfile ("C: \ myjstest.txt", true "); |
Alert ("file last modified:" + f1.datelastmodified ); |
It is easy to use FileSystemObject objects to program drive and folder operations, just like performing interactive operations on files in a Windows file browser, such as copying and moving folders, obtain the attributes of a folder. |
(1) drives Object Attributes |
The drive object collects the content of physical or logical drive resources in the system. It has the following attributes: |
L totalsize: the size of the drive in bytes. |
L availablespace or freespace: The drive space in bytes. |
L driveletter: drive letter. |
L drivetype: Drive Type, value: Removable (Mobile Medium), fixed (fixed medium), Network (Network Resources), CD-ROM or ramdisk. |
L serialnumber: the serial number of the drive. |
L filesystem: The file system type of the drive. Values: fat, FAT32, and NTFS. |
L isready: whether the drive is available. |
L volumename: the name of the volume label. |
L path and rootfolder: The drive path or root directory name. |
(2) Drive object operation routine |
The following routine displays the volume label, total capacity, and available space of drive C: |
FSO = new activexobject ("scripting. FileSystemObject "); |
DRV = FSO. getdrive (FSO. getdrivename ("C :\\")); |
S + = DRV. volumename + "\ n "; |
S + = "total space:" + DRV. totalsize/1024; |
S + = "Free Space:" + DRV. freespace/1024; |
The following message box appears after execution: |
|
Folder operations include creating, moving, deleting, and obtaining related properties. |
(1) List of related attributes and methods of the folder object Filesystemobjec. createfolder Folder. delete or filesystemobjec. deletefolder delete a folder Folder. Move or filesystemobjec. movefolder move folder Folder. Copy or filesystemobjec. copyfolder Folder. name to get the folder name Filesystemobjec. folderexists Filesystemobjec. getfolder Filesystemobjec. getparentfoldername: Get the parent folder Name of the folder. Filesystemobjec. getspecialfolder |
(2) folder object operation routine |
The following routine obtains the parent folder name, creates a folder, deletes a folder, and determines whether the folder is the root directory: |
// Create a FileSystemObject object instance |
FSO = new activexobject ("scripting. FileSystemObject "); |
FLDR = FSO. getfolder ("C :\\"); |
// Display the parent directory name |
Alert ("parent folder name is:" + FLDR + "\ n "); |
// Display the drive name |
Alert ("contained on drive" + FLDR. Drive + "\ n "); |
// Determine whether the root directory is used |
Alert ("this is the root folder ."); |
Alert ("this folder isn't a root folder ."); |
FSO. createfolder ("C: \ bogus "); |
Alert ("created folder C: \ bogus" + "\ n "); |
// Display the basic folder name, excluding the path name |
Alert ("basename =" + FSO. getbasename ("C: \ bogus") + "\ n "); |
// Delete the created folder |
FSO. deletefolder ("C: \ bogus "); |
Alert ("deleted folder C: \ bogus" + "\ n "); |
The operations on files are more complex than the drive and folder operations described above. They are basically divided into the following two categories: create, copy, move, and delete objects, and create, add, delete, and read objects. The following describes in detail. |
There are three methods to create an empty text file, which is also called a text stream ). |
The first method is to use the createtextfile method. The Code is as follows: |
FSO = new activexobject ("scripting. FileSystemObject "); |
F1 = FSO. createtextfile ("C: \ testfile.txt", true ); |
The second method is to use the opentextfile method and add the forwriting attribute. The value of forwriting is 2. The Code is as follows: |
FSO = new activexobject ("scripting. FileSystemObject "); |
TS = FSO. opentextfile ("C: \ test.txt", forwriting, true ); |
The third method is to use the openastextstream method and set the forwriting attribute. The Code is as follows: |
FSO = new activexobject ("scripting. FileSystemObject "); |
FSO. createtextfile ("C: \ test1.txt "); |
F1 = FSO. GetFile ("C: \ test1.txt "); |
TS = f1.openastextstream (forwriting, true ); |
After a file is created, you can add data to the file by following the steps of "Open File> Fill in data> close file. |
To open a file, you can use the opentextfile method of the FileSystemObject object or the openastextstream method of the file object. |
Use the write, writeline, or writeblanklines methods of the textstream object to fill in the data. With the same function of writing data, the difference between the three is that the write method does not add a new line break at the end of the written data, and the writeline method must add a new line break at the end, writeblanklines adds one or more empty lines. |
To close a file, use the close method of the textstream object. |
(3) create a file and add a data routine |
The following code combines the steps of creating a file, adding data, and closing a file for application: |
FSO = new activexobject ("scripting. FileSystemObject "); |
TF = FSO. createtextfile ("C: \ testfile.txt", true ); |
// Fill in the data and add a line break |
TF. writeline ("Testing 1, 2, 3 ."); |
TF. writeblanklines (3 ); |
// Enter a line without a line break |
TF. Write ("this is a test ."); |
To read data from a text file, use the read, Readline, or readall methods of the textstream object. The read method is used to read a specified number of characters in a file. The Readline method reads a whole line, but does not include line breaks. The readall method reads the entire content of a text file. The read content is stored in string variables for display and analysis. When reading the file content using the read or Readline method, if you want to skip some parts, you need to use the skip or skipline method. |
The following code demonstrates opening a file, entering data, and then reading data: |
FSO = new activexobject ("scripting. FileSystemObject "); |
F1 = FSO. createtextfile ("C: \ testfile.txt", true ); |
F1.writeline ("Hello World "); |
TS = FSO. opentextfile ("C: \ testfile.txt", forreading ); |
// Read a row of content from the file to a string |
// Display string Information |
Alert ("File Contents = '" + S + "'"); |
(5) moving, copying, and deleting objects |
For the above three file operations, JavaScript has two corresponding methods: file. move or FileSystemObject. movefile is used to move files. File. copy or FileSystemObject. copyfile is used to copy files. File. delete or FileSystemObject. deletefile is used to delete objects. |
The following code creates a text file under the root directory of drive C, fills in some content, moves the file to the \ tmp directory, and creates a file copy under the \ temp directory, finally, delete the files in these two directories: |
FSO = new activexobject ("scripting. FileSystemObject "); |
F1 = FSO. createtextfile ("C: \ testfile.txt", true ); |
F1.write ("this is a test ."); |
// Obtain the file handle under the c: \ root directory |
F2 = FSO. GetFile ("C: \ testfile.txt "); |
// Move the file to the \ tmp directory |
F2.move ("C: \ TMP \ testfile.txt "); |
// Copy the file to the \ Temp Directory |
F2.copy ("C: \ temp \ testfile.txt "); |
// Obtain the file handle |
F2 = FSO. GetFile ("C: \ TMP \ testfile.txt "); |
F3 = FSO. GetFile ("C: \ temp \ testfile.txt "); |
|