Javascript-File Operations

Source: Internet
Author: User

Javascript-File Operations
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
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 );
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 );
4. 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 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.
Related Article

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.