[Paste] JavaScript-File Operations

Source: Internet
Author: User

This article is reprinted from
Http://www.late-fall.com/forum/archiver? Tid-613.html
Thank you very much for the original author.

I. function implementation core: FileSystemObject object
To implement file operations in Javascript, FileSystemObject objects are primarily used.
Ii. FileSystemObject Programming
It is easy to program with FileSystemObject object,
Follow these steps:
Create a FileSystemObject object, application-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,
This must be achieved through the get series method: getdrive is responsible for obtaining the drive information,
Getfolder is responsible for obtaining Folder Information, while GetFile is responsible for obtaining 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 that:
For objects created using the create method, you no longer need to use the get method to obtain the object handle,
In this case, you can directly use the Create method to create a handle name:
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,
This is like interacting with files in a Windows file browser,
For example, copy or move a folder to 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)
Operations on files are compared to the drive described above)
And folder operations are more complex,
There are basically two categories:
Create, copy, move, and delete objects
And create, add, delete, and read file content.
The following describes in detail.
(1) create a file
There are three methods for creating an empty text file,
Such files are sometimes called text streams ).
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,
Generally, you need to add data to the file by following the steps of "Open File> enter data> close file.
To open a file, use the opentextfile method of the FileSystemObject object,
Or use the openastextstream method of the file object.
Use the write, writeline, or writeblanklines methods of the textstream object to fill in the data.
While writing data,
The difference between the three is:
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,
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 the read or Readline method is used to read the file content,
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 for moving 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,
Enter some content and move the file to the \ tmp directory,
Create 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 various objects, attributes, and methods of FileSystemObject,
I believe you have been operating on the drive,
Files and folders have a clear understanding. However, the routines mentioned above are very simple,
A large number of practical exercises are required to fully and flexibly master the Javascript file operation technology.
In addition, I would like to remind you that advanced operations such as reading and writing files in a browser are involved,
For the default browser security level, a message is prompted before the code is run,
Note this in the actual environment.

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.