JavaScript Advanced Application: File Operations

xiaoxiao2021-03-06  42

JavaScript is a scripting language that is not open from the web page, relying on it, a web page content is lively, rich in gas. But maybe you haven't found and apply some more and more features? For example, read, write, and delete files and folders, just like often work in high-level languages ​​such as VB, VC. How, do you need to understand this knowledge? Then please come with me, this article will describe how to use the JavaScript language to perform file operations.

First, functional implementation core: FileSystemObject object

In fact, to implement file operation functions in JavaScript, mainly on the FileSystemObject object. Let's take a look at the objects and collections of this object, in a detailed introduction to the various properties and methods of the FILESystemObject object.

Second, FILESYSTEMOBJECT programming three songs

Programming using the FileSystemObject object is simple, usually the following steps: Create a FileSystemObject object, apply related methods, access object-related properties.

(1) Creating a FileSystemObject object

Creating a FileSystemObject object as long as 1 line:

VAR fso = new activxObject ("scripting.filesystemObject");

After the above code is executed, the FSO is an instance of a FILESystemObject object.

(2) Application related methods

Once you have created an object instance, you can use the object's related ways. For example, create a text file using the CreateTextFile method:

VAR fso = new activxObject ("scripting.filesystemObject");

Var f1 = fso.createtextfile ("c: //myjstest.txt", true ");

(3) Access object related properties

To access the object's related properties, first create a handle of the object, this is to implement the GET series method: getDrive is responsible for obtaining the drive information, getFolder is responsible for obtaining folder information, getFile is responsible for obtaining file information. For example, after the code below, F1 becomes a handle of the file c: /test.txt:

VAR fso = new activxObject ("scripting.filesystemObject");

Var f1 = fso.getfile ("c: //myjstest.txt");

Then, use the F1 access to the relevant properties of the object. such as:

VAR fso = new activxObject ("scripting.filesystemObject");

Var f1 = fso.getfile ("c: //myjstest.txt");

Alert ("File Last Modified:" F1.DateLastModified;

After performing the last sentence above, the final modification date attribute value of C: /MYJSTEST.TXT will be displayed.

But one thing note: For objects established using the CREATE method, you don't have to use the GET method to get the object handle, then you can use the crete method to create the handle name:

VAR fso = new activxObject ("scripting.filesystemObject");

Var f1 = fso.createtextfile ("c: //myjstest.txt", true "); Alert (" File Last Modified: " f1.dateLastModified;

Third, operating drive (drives)

Using the FileSystemObject object to program an operation drive (airs) and folders (Folders), it is easy to interact with files in the Windows file browser, such as copy, moving folders, and get the properties of the folder.

(1) DRIVES object attribute

The Drive object is responsible for collecting physical or logical drive resources in the system, which has the following properties:

l Totalsize: The drive size calculated in byte (byte).

l AvailableSpace or FreeSpace: The drive is available in byte (Byte).

l Driveletter: Drive letters.

l DriveType: The drive type, the value is: Removable (mobile media), Fixed (fixed media), NetWork, CD-ROM, or RAM disk.

l SerialNumber: The series code of the drive.

l FileSystem: The file system type of the drive is in FAT, FAT32, and NTFS.

l isReady: Whether the drive is available.

l ShareName: Shared name.

l Volumename: Volume Boot Name.

l Path and rootfolder: The path or root directory name of the drive.

(2) DRIVE object operation routine

The following routines displays information about the volume label, total capacity, and available space of the drive C:

VAR FSO, DRV, S = ""

Fso = New ActiveXObject ("scripting.filesystemObject");

DRV = fso.getdrive (fso.getdrivename);

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);

The following message box appears after execution:

Fourth, operation folder (Folders)

The operations involving folders include creation, movement, delete, and obtain related properties.

(1) List of related properties and methods of Folder objects

(2) Folder object operation routine

The following routines will practice to obtain the parent folder name, create a folder, delete folder, and determine whether it is root directory:

VAR FSO, FLDR, S = ""

// Create a FileSystemObject object instance fso = new activXObject ("scripting.filesystemObject);

/ / Get a 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");

/ / Judgment is the root directory

Fldr.isrootfolder

"" this is the root folder. ");

Else

Alert ("This Folder isn't a root folder.");

Alert ("/ n / n");

// Create a new folder

Fso.createfolder ("c: // bogus");

Alert ("CREATED Folder C: // Bogus" "/ N");

// Display the folder base name, does not include the path name

Alert ("BaseName =" fso.getBaseName ("c: // bogus") "/ n");

/ / Delete the created folder

Fso.Deletefolder ("C: // Bogus");

Alert ("Deleded Folder C: // Bogus" "/ N");

V. Operation file (files)

The operation of the file is complicated than the drive (DRIVE) and folder (Folder) described above, basically divided into two categories: creation, copy, mobile, delete operation, and creation of file content , Add, delete, and read operations. The following details are described in detail below.

(1) Creating a file

There are three ways to create an empty text file, which is sometimes called text stream.

The first is to use the CreateTextFile method. code show as below:

VAR FSO, F1;

Fso = New ActiveXObject ("scripting.filesystemObject");

F1 = fso.createtextfile ("c: //testfile.txt", true);

The second is to use the OpenTextFile method and add the ForWriting property, the value of ForWriting is 2. code show as below:

VAR FSO, TS;

Var forwriting = 2;

Fso = New ActiveXObject ("scripting.filesystemObject");

Ts = fso.opentextfile ("c: //test.txt", forwriting, true);

The third is to use the OpenASTextStream method, you can also set the Forwriting property. code show as below:

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

When the file is created, you should generally follow the "Open File-> Fill Data -> Off File" step to achieve the purpose of adding data to the file.

Open the file Use the OpenTextFile method of the FileSystemObject object, or the OpenASTextStream method for the File object.

Fill in the data to use the Write, WriteLine or Writeblanklines method for the TextStream object. Under the function of implementing write data, the three differences are that the Write method does not add new wraps at the end of the data, and the WriteLine method is to add a new wrap, while WriteBlanklines adds one or more empty. Row.

Closing the file You can use the Close method of the TextStream object.

(3) Creating files and adding data routines

The following code will create a file, add data, and turn off files to combine several steps:

VAR FSO, TF;

Fso = New ActiveXObject ("scripting.filesystemObject");

// Create a new file

Tf = fso.createtextfile ("c: //testfile.txt", true);

// Fill in the data and increase the newline

Tf.writeline ("Testing 1, 2, 3.");

// Add 3 empty rows

Tf.writeblanklines (3);

// Fill in a row, do not have a newline

Tf.write ("this is a test.");

// Turn the file

Tf.close ();

(4) Read the contents of the file

Read data from a text file To use the Read, Readline or Readall method of the TextStream object. The READ method is used to read the specified number of characters in the file; the Readline method reads a single line, but does not include a newline method; the Readall method reads the entire content of the text file. The read content is stored in a string variable for display, analysis. When reading the file content using the Read or Readline method, if you want to skip some parts, you should use the SKIP or Skipline method.

The following code presentation opens the file, fill in the data, and then read the data:

VAR FSO, F1, TS, S;

Var forreading = 1;

Fso = New ActiveXObject ("scripting.filesystemObject");

// Create a file

F1 = fso.createtextfile ("c: //testfile.txt", true);

// Fill in a line of data

F1.writeline ("Hello World");

f1.writeblanklines (1);

// Turn the file

f1.close ();

// open a file

Ts = fso.opentextfile ("c: //testfile.txt", forreading);

// read the file a line of content to the string

S = ts.readline ();

// Display string information

Alert ("File Contents = '" S "");

// Close the file ts.close ();

(5) Moving, copying and deleting files

For the above three file operations, JavaScript has two corresponding methods: file.move or filesystemObject.movefile is used for mobile files; file.copy or filesystemObject.copy is used for copy files; file.delete or filesystemObject.deletefile is used to delete file.

The following code demonstrates a text file in the root of the drive C, fill in some content, then move the file to the / TMP directory, then create a file copy below the directory / TEMP, finally delete the files of these two directories:

VAR FSO, F1, F2, S;

Fso = New ActiveXObject ("scripting.filesystemObject");

F1 = fso.createtextfile ("c: //testfile.txt", true);

// Write a line

F1.write ("this is a test.");

// Turn the file

f1.close ();

/ / Get the file handle under the C: / root directory

F2 = fso.getfile ("c: //testfile.txt");

// Mobile file to / TMP directory

F2.move ("c: //tmp/testfile.txt");

// Copy file to / temp directory

F2.copy ("c: //temp/testfile.txt");

/ / Get the file handle

F2 = fso.getfile ("c: //tmp/testfile.txt");

F3 = fso.getfile ("c: //temp/testfile.txt");

// Delete Files

f2.delete ();

f3.delete ();

Sixth, junction

Through the introduction and examples of various objects, properties, and methods of FileSystemObject, I believe that you have clearly understood how to operate drives, files, and folders in the page using JavaScript language. However, the routines mentioned above are very simple, to be comprehensive, flexible to master JavaScript file operation technology, and require a lot of practical practice. And some remind everyone that because of the advanced operation of reading such a file in the browser, for the default browser security level, there will be a message prompt before the code is running, please in the actual environment Tip visitor pay attention.

转载请注明原文地址:https://www.9cbs.com/read-113208.html

New Post(0)