Javascript is a scripting language that is indispensable for creating web pages. With JavaScript, the content of a Web page is lively and vigorous. But maybe you haven't found and applied some of its more advanced features yet? For example, reading, writing, and deleting files and folders is the same as reading, writing, and deleting files and folders in advanced languages such as VB and VC. How do you know about this? Please come with me. This article will describe in detail how to use JavaScript to perform file operations.
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:
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
Create a FileSystemObject objectCodeJust 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. OperationCompositionFolder (folders)
Folder operations include creating, moving, deleting, and obtaining related properties.
(1) List of related attributes and methods of the folder object
(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:
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. You must also 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 generally need to 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:
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 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:
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 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:
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
Through the introduction and examples of FileSystemObject's various objects, attributes, and methods, I believe you have a clear understanding of how to use JavaScript to operate drives, files, and folders on pages. 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. In addition, we would like to remind you that, due to advanced operations such as file reading and writing in the browser, there will be a message before the code is run for the default browser security level, note this in the actual environment.