Javascript file operations File Object
I. function implementation core: FileSystemObject object
To implement file operations in Javascript, FileSystemObject objects are primarily used.
Ii. FileSystemObject Programming
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
Create a FileSystemObject object Code Just one line:
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 );
3. Drive)
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 sharename: Share Name.
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:
VaR FSO, DRV, S = "";
FSO = new activexobject ("scripting. FileSystemObject ");
DRV = FSO. getdrive (FSO. getdrivename ("C :\\"));
S + = "drive C:" + "-";
S + = DRV. volumename + "\ n ";
S + = "total space:" + DRV. totalsize/1024;
S + = "kb" + "\ n ";
S + = "Free Space:" + DRV. freespace/1024;
S + = "kb" + "\ n ";
Alert (s );
Iv. Operation Composition Folder (folders)
Folder operations include creating, moving, deleting, and obtaining related properties.
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:
VaR FSO, FLDR, S = "";
// Create a FileSystemObject object instance
FSO = new activexobject ("scripting. FileSystemObject ");
// Get the drive object
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
If (FLDR. isrootfolder)
Alert ("this is the root folder .");
Else
Alert ("this folder isn't a root folder .");
Alert ("\ n ");
// Create a new 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 ");
5. Files)
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.
(1) create a file
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:
VaR FSO, F1;
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:
VaR FSO, TS;
VaR forwriting = 2;
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:
VaR FSO, F1, TS;
VaR forwriting = 2;
FSO = new activexobject ("scripting. FileSystemObject ");
FSO. createtextfile ("C: \ test1.txt ");
F1 = FSO. GetFile ("C: \ test1.txt ");
TS = f1.openastextstream (forwriting, true );
(2) Add data to a file
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 use File Openastextstream method of the object.
Use the write, writeline, or writeblanklines methods of the textstream object to fill in the data. Write Data in the same way
Under, the 3
The difference is that the write method does not add a new line break at the end of the written data, the writeline method must add a new line break at the end, and the writeblanklines method increases
One or more empty rows.
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:
VaR FSO, TF;
FSO = new activexobject ("scripting. FileSystemObject ");
// Create a new file
TF = FSO. createtextfile ("C: \ testfile.txt", true );
// Fill in the data and add a line break
TF. writeline ("Testing 1, 2, 3 .");
// Add 3 empty rows
TF. writeblanklines (3 );
// Enter a line without a line break
TF. Write ("this is a test .");
// Close the file
TF. Close ();
(4) Reading File Content
To read data from a text file, use the read, Readline, or readall of the textstream object.
Method. 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. Read
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 skip or
The skipline method.
The following code demonstrates opening a file, entering data, and then reading data:
VaR FSO, F1, ts, S;
VaR forreading = 1;
FSO = new activexobject ("scripting. FileSystemObject ");
// Create a file
F1 = FSO. createtextfile ("C: \ testfile.txt", true );
// Enter a data row
F1.writeline ("Hello World ");
F1.writeblanklines (1 );
// Close the file
F1.close ();
// Open the file
TS = FSO. opentextfile ("C: \ testfile.txt", forreading );
// Read a row of content from the file to a string
S = ts. Readline ();
// Display string Information
Alert (" File Contents = '"+ S + "'");
// Close the file
TS. Close ();
(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 objects; File . Delete or FileSystemObject. deletefile is used to delete files.
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:
VaR FSO, F1, F2, S;
FSO = new activexobject ("scripting. FileSystemObject ");
F1 = FSO. createtextfile ("C: \ testfile.txt", true );
// Write a row
F1.write ("this is a test .");
// Close the file
F1.close ();
// 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 ");
// Delete an object
F2.delete ();
F3.delete ();
Vi. Closed speech
Based on the introduction and examples of various objects, attributes, and methods of FileSystemObject, I believe you have used JavaScript to operate the drive on pages.
The animation, file, and folder have a clear understanding. However, the routines mentioned above are very simple. To fully and flexibly master the Javascript file operation technology, a lot of practical exercises are required. And also
I would like to remind you that, as it involves advanced operations such as reading and writing files in a browser, there will be a message before the code runs for the default browser security level, in the actual environment
Prompt the visitor's attention.
File Object
Provides access to all attributes of a file.
Description
The following code demonstrates how to obtainFileObject and how to view its attributes.
Function showfileinfo (filespec) {var FSO, F, S; FSO = new
Activexobject ("scripting. FileSystemObject"); F =
FSO. GetFile (filespec); s = f. datecreated; Return (s );}
Method
COPY method | Delete method | move method | openastextstream Method
Attribute
attributes attribute | datecreated attribute | datelastaccessed attribute | datelastmodified
attribute | drive attribute | Name attribute | parentfolder attribute | path attribute | shortname attribute | export path
attribute | size attribute | type attribute
See