Javascript-webpage Client System File Operations-FileSystemObject object

Source: Internet
Author: User
I. Core functions: FileSystemObject objects. To implement file operations in Javascript, FileSystemObject objects are primarily used.
Ii. FileSystemObject programming Trilogy

(1) create a FileSystemObject object, application-related methods, and Access Object-related attributes.
VaR FSO = new activexobject ("scripting. FileSystemObject ");
(2) Use the createtextfile method to create a text file:
VaR F1 = FSO. createtextfile ("C: \ myjstest.txt", true ");
(3) to access object-related attributes, you must first create a handle pointing to the object, which must be implemented through the get series method:
Getdrive obtains the drive information,
Getfolder is responsible for obtaining Folder Information,
GetFile obtains the file information.

VaR FSO = new activexobject ("scripting. FileSystemObject ");
VaR F1 = FSO. GetFile ("C: \ myjstest.txt"); // After pointing to the following code, F1 becomes the handle pointing to the file c: \ test.txt:

Alert ("file last modified:" + f1.datelastmodified); // use F1 to Access Object Attributes
The Last modified Date attribute value of C: \ myjstest.txt is displayed.

If you use the Create method to create an object, you do not need to use the get method to obtain the object handle. 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)
Using FileSystemObject objects to program drive and folder operations is easy

(1) drives object attributes: The drive object collects the content of physical or logical drive resources in the system. It has the following attributes:
Totalsize: the size of the drive in bytes.
Availablespace or freespace: The drive space in bytes.
Driveletter: drive letter.
Drivetype: Drive Type; optional values: removable, fixed, network, CD-ROM, or ramdisk.
Serialnumber: the serial number of the drive.
Filesystem: The file system type of the drive. Values: fat, FAT32, and NTFS.
Isready: whether the drive is available.
Sharename: Share Name.
Volumename: volume name.
Path and rootfolder: The drive path or root directory name.

(2) Drive object operation: the volume label, total capacity, and available space of drive C are displayed below:
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 );

4. Folders)
Folder operations include creating, moving, deleting, and obtaining related properties.
Folder object operation 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 = "";
FSO = new activexobject ("scripting. FileSystemObject"); // create a FileSystemObject object instance
FLDR = FSO. getfolder ("C :\\"); // get the drive object
Alert ("parent folder name is:" + FLDR + "\ n"); // display the parent directory name
Alert ("contained on drive" + FLDR. Drive + "\ n"); // display the name of the drive
If (FLDR. isrootfolder) // determines whether the root directory is used.
Alert ("this is the root folder .");
Else
Alert ("this folder isn't a root folder .");
Alert ("\ n ");
FSO. createfolder ("C: \ bogus"); // create a folder
Alert ("created folder C: \ bogus" + "\ n ");
Alert ("basename =" + FSO. getbasename ("C: \ bogus") + "\ n"); // display the basic name of the folder, excluding the path name
FSO. deletefolder ("C: \ bogus"); // Delete the created folder
Alert ("deleted folder C: \ bogus" + "\ n ");

5. Create, copy, move, and delete files, and create, add, delete, and read files.
 
(1) create a file
A total of three methods can be used to create an empty text file. [1] Such a file is also called a text stream ).
VaR FSO, F1;
FSO = new activexobject ("scripting. FileSystemObject ");
F1 = FSO. createtextfile ("C: \ testfile.txt", true); Use the createtextfile Method
[2]
VaR FSO, TS;
VaR forwriting = 2;
FSO = new activexobject ("scripting. FileSystemObject ");
TS = FSO. opentextfile ("C: \ test.txt", forwriting, true); // use the opentextfile method and add the forwriting attribute. The value of forwriting is 2.
[3]
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); // use the openastextstream method and set the forwriting attribute.

(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 the openastextstream method of the file object.
Use the write, writeline, or writeblanklines methods of the textstream object to fill in the data.
The write method does not add a new line break at the end of the written data.
Add a new line break to the last part of the writeline method.
Writeblanklines adds 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
VaR FSO, TF;
FSO = new activexobject ("scripting. FileSystemObject ");
TF = FSO. createtextfile ("C: \ testfile.txt", true); // create a new file
TF. writeline ("Testing 1, 2, 3."); // enter the data and add a line break.
TF. writeblanklines (3); // Add 3 blank lines.
TF. Write ("this is a test."); // enter a line without a line break.
TF. Close (); // close the file

(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.

VaR FSO, F1, ts, S;
VaR forreading = 1;
FSO = new activexobject ("scripting. FileSystemObject ");
F1 = FSO. createtextfile ("C: \ testfile.txt", true); // create a file
F1.writeline ("Hello World"); // enter a data row
F1.writeblanklines (1 );
F1.close (); // close the file
TS = FSO. opentextfile ("C: \ testfile.txt", forreading); // open the file
S = ts. Readline (); // read a line of content from the file to the string
Alert ("File Contents = '" + S + "'"); // display string Information
TS. Close (); // close the file

(5) moving, copying, and deleting objects
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.

VaR FSO, F1, F2, S;
FSO = new activexobject ("scripting. FileSystemObject ");
F1 = FSO. createtextfile ("C: \ testfile.txt", true );
F1.write ("this is a test."); // write a row.
F1.close (); // close the file
F2 = FSO. GetFile ("C: \ testfile.txt"); // obtain the file handle in the C: \ root directory
F2.move ("C: \ TMP \ testfile.txt"); // move the file to the \ tmp directory.
F2.copy ("C: \ temp \ testfile.txt"); // copy the file to the \ temp directory.
F2 = FSO. GetFile ("C: \ TMP \ testfile.txt"); // obtain the file handle
F3 = FSO. GetFile ("C: \ temp \ testfile.txt ");
F2.delete (); // delete an object
F3.delete ();

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.