Title JavaScript ---- File Operation Zhaoweiwei (Favorites) Keyword JavaScript
First, functional implementation: FileSystemObject object To implement file operation functions in JavaScript, mainly on the FileSystemObject object. Second, FILESYSTEMOBJECT programming is easy to program with the FileSystemObject object, generally to pass the following steps: Create a FileSystemObject object, apply related methods, access object-related properties. (1) Creating a FileSystemObject object Create a FileSystemObject object code as long as 1 line: var fso = new activXObject ("scripting.filesystemObject"); After the code is executed, the FSO is an instance of a FileSystemObject object. (2) After the application of the relevant method creates an object instance, the relevant method of the object can be used. For example, create a text file using the CreateTextFile method: var fso = new activityXObject ("scripting.filesystemObject"); var f1 = fso.createtextFile ("c: //myjstest.txt", true "); (3) Access objects related Attributes To access the object's related properties, first create the handle of the object, this is to implement by GETDRIVE, get the drive information, getFolder is responsible for obtaining folder information, getFile is responsible for obtaining file information. For example, point the code below After, F1 is a handle of the file c: /test.txt: var fso = new activiXObject ("scripting.filesystemObject"); var f1 = fso.getfile ("c: //myjstest.txt"); then, use F1 access to the object's related properties. For example: var fso = new activiXObject ("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. However, please note: For an object established using the CREATE method, you don't have to use the GET method to get the object handle, At this time, you can use the handle name created directly by the create method: var fso = new activiXObject ("scripting.filesystemObject"); var f1 = fso.createtextfile ("c: //myjstest.txt", true "); Alert (" File Last Modified: " f1.dateLastModified; 3, Operation Drive (Drives) Use FileSystemObject Objects to program the Operation Drive (DRIVES) and Folders (Folders) Easy, this is like interacting files in the Windows file browser Like operation, such as: copy, moving folders, get the properties of the folder.
(1) DRIVES object properties Drive objects are 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 routines: VAR FSO, DRV, S = ""; fso = new activityXObject ("scripting.FilesystemObject); DRV = Fso.getdrive ("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 ); Fourth, the operation folder involves the operation of the folder includes creation, movement, delete, and obtain related properties.
Folder object operation routine: The following routines will practice get the parent folder name, create folders, delete folders, and determine if it is root directory, etc.: VAR FSO, FLDR, S = ""; // Create a FileSystemObject object instance Fso = new activityXObject ("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 "); // Judgment is the root directory IF (FldroOTFolder) Alert (" 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"); // Displays the folder base name, does not include path name Alert ("BaseName =" fso.getBaseName ("c: // bogus") " / n "); // Delete the created folder fso.deletefolder (" c: // bogus "); Alert (" deleted folder c: // bogus "/ n"); 5, 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 total of three ways can be used to create an empty text file, which is sometimes called text stream. The first 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 is to use the OpenTextFile method and add it Forwriting properties, the value of ForWriting is 2. The code is as follows: VAR FSO, TS; var forwriting = 2; fso = new activXObject ("scripting.filesystemObject"); ts = fso.opentextfile ("c: //test.txt", forting, true); third is Use the OpenASTextStream method to set the ForWriting property.
The code is as follows: VAR FSO, F1, TS; var forwriting = 2; fso = new activXObject ("scripting.filesystemObject"); fso.createtextfile ("c: //test1.txt"); f1 = fso.getfile ("c : //test1.txt "); ts = f1.openastextStream (forWriting, True); (2) Adding data to a file When the file is created, the step of" Open File -> Fill in Data -> Close File " Implement 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 a file and adding the code under the data routine will create a file, add data, close files to combine several steps: VAR FSO, TF; FSO = New ActiveXObject ("scripting.filesystemObject"); // creation New file tf = fso.createtextFile ("c: //testfile.txt", true); // Fill in the data, and increase the wrap Tf.Writeline ("Testing 1, 2, 3."); // Add 3 Dark line Tf.WriteBlankLines (3); // Fill in a row, without a wrapper Tf.Write ("this is a test."); // Close the file tf.close (); (4) Read the file content from text Reading data in the 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.