This is the 16th day of my participation in the August Gwen Challenge **

On the Windows platform, JS can call a lot of ActivexObject provided by Windows, this paper uses JS to achieve document processing, and use JS to write ActiveX to do a simple introduction.

The ActiveXObject object in JavaScript is a reference to the enabled and returned Automation object. Usage:

newObj = new ActiveXObject( servername.typename[, location])
Copy the code

The ActiveXObject syntax has these parts: newObj is mandatory. The variable name to assign to ActiveXObject. Servername is mandatory. The name of the application that provides the object. Typename is mandatory. The type or class of the object to be created. Location is optional. The name of the network server on which the object was created.

Remember: ActiveX is a Microsoft product, so only Internet Explorer supports it!

Use ActiveXObject in javaScript to create FileSystemObject operation file FileSystemObject to achieve file operation functions in javascript, mainly depends on FileSystemObject. Using FileSystemObject programming is very simple, generally through the following steps:

Create FileSystemObject, apply related methods, and access object attributes.

(a) create FileSystemObject object create FileSystemObject object code as long as the line 1: the var fso = new ActiveXObject (” Scripting. FileSystemObject “); After the above code is executed, the FSO becomes an instance of FileSystemObject. (two) after the application of the relevant method to create an object instance, you can use the object of the relevant method. For example, create a text file using the CreateTextFile method:

var fso = newActiveXObject (" Scripting. FileSystemObject ");varF1 = fso. Createtextfile (" c: \ \ myjstest TXT ",true");Copy the code

(3) Access the object related attributes to access the object related attributes, the first to establish a handle to the object, which is achieved through a series of get methods: GetDrive is responsible for obtaining drive information, GetFolder is responsible for obtaining folder information, GetFile is responsible for obtaining file information. For example, after pointing to the following code, f1 becomes a handle to the file c:\test.txt:

var fso = newActiveXObject (" Scripting. FileSystemObject ");varF1 = fso. GetFile (" c: \ \ myjstest TXT "); Then, use F1 to access the relevant properties of the object. Such as:var fso = newActiveXObject (" Scripting. FileSystemObject ");varF1 = fso. GetFile (" c: \ \ myjstest TXT "); Alert (" File LastModified: "+ f1.datelastModified);Copy the code

After executing the last sentence above, the value of the last modified date property of c:\myjstest. TXT is displayed. Create (); get (); create (); getHandle ();

var fso = newActiveXObject (" Scripting. FileSystemObject ");varF1 = fso. Createtextfile (" c: \ \ myjstest TXT ",true"); Alert (" File LastModified: "+ f1.datelastModified);Copy the code

Using FileSystemObject to programmatically operate Drives and Folders (Folders) is very easy, just like in the Windows file browser for file interactive operations, such as: copy, move Folders, obtain folder properties. Drive Collects physical and logical Drive resources in the system. It has the following properties: l TotalSize: Specifies the Drive size in bytes. L AvailableSpace or FreeSpace: the AvailableSpace of the drive in bytes. L DriveLetter: DriveLetter. L DriveType: specifies the DriveType. Its property can be removable, fixed, network, cd-rom, or RAM. L SerialNumber: SerialNumber of the drive. L FileSystem: specifies the FileSystem type of the drive. The value can be FAT, FAT32, or NTFS. L IsReady: Indicates whether the drive is available. L ShareName: indicates the ShareName. L VolumeName: indicates the volume label name. L Path and RootFolder: indicates the Path or root directory name of the drive. The following routine displays the volume label, total capacity, and available space of Drive C:

varFso, 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 += "FreeSpace:" + drv.freespace / 1024; S += "Kb" + "\n"; alert(s);Copy the code

Operations related to Folders include creating, moving, deleting and obtaining related properties. Folder object operation routines: The following routines will practice getting the name of the parent Folder, creating folders, deleting folders, and determining whether the Folder is the root directory:

varFso, FLDR, s = ";// Create FileSystemObject instances
fso = newActiveXObject (" Scripting. FileSystemObject ");// Get the Drive objectFLDR = fso. GetFolder (" c: \ \ ");// Displays the parent directory nameAlert (" Parent Folder name is: "+ FLDR +" \n ");// Displays the name of the driveAlert (" Contained on Drive "+ FLdr. drive +" \n ");// Check whether the directory is the root directory
if(fldr.IsRootFolder) Alert (" This is the root folder. ");elseAlert (" This Folder isn't a root folder. "); Alert (" \ n \ n ");// Create a new folderFso. CreateFolder (" C: \ \ Bogus "); Alert (" Created Folder C:\\Bogus "+" \n ");// Displays the base folder name, excluding the pathnameAlert (" the Basename = "+ fso. GetBaseName (" c: \ \ bogus") + "\ n");// Delete the created folderFso. DeleteFolder (" C: \ \ Bogus "); Alert (" Deleted Folder C:\\Bogus "+" \n ");Copy the code

Operations on Files are more complex than those on Drive and Folder. They are basically divided into the following two types: creating, copying, moving, and deleting Files and creating, adding, deleting, and reading Files. Details are given below. There are three ways to create an empty text file, which is sometimes called a text stream. The first is to use the CreateTextFile method. The code is as follows:

var fso, f1;
fso = newActiveXObject (" Scripting. FileSystemObject "); F1 = fso. CreateTextFile (" c: \ \ testfile TXT ",true); The second option is to use the OpenTextFile method and add the ForWriting attribute, with the value of ForWriting2. The code is as follows:var fso, ts;
var ForWriting= 2;
fso = newActiveXObject (" Scripting. FileSystemObject "); Ts = fso. OpenTextFile (" c: \ \ test TXT ", ForWriting,true); The third way is to use the OpenAsTextStream method, again with the ForWriting property set. The code is as follows:var fso, f1, ts;
var ForWriting = 2;
fso = newActiveXObject (" Scripting. FileSystemObject "); Fso. CreateTextFile (" c: \ \ test1 TXT "); F1 = fso. GetFile (" c: \ \ test1 TXT "); ts = f1.OpenAsTextStream(ForWriting,true);
Copy the code

(2) Adding data to a file After a file is created, you must follow the steps of “Open file -> Fill In Data -> Close File” to add data to the file. You can open files using the OpenTextFile method of FileSystemObject or the OpenAsTextStream method of File object. Fill in the data using the Write, WriteLine, or WriteBlankLines methods of the TextStream object. Write does not add a new line break at the end of a Write, WriteLine adds a new line break at the end, and WriteBlankLines adds one or more blank lines. The file can be closed using the Close method of the TextStream object. The following code combines the steps of creating a file, adding data, and closing a file:

var fso, tf;
fso = newActiveXObject (" Scripting. FileSystemObject ");// Create a new fileTf = fso. CreateTextFile (" c: \ \ testfile TXT ",true);
// Fill in the data and add a newline characterTf. WriteLine (" Testing1.2.3.");// Add 3 blank lines
tf.WriteBlankLines(3);// Fill in one line without a newline characterTf.write (" This is a test. ");// Close the file
tf.Close();
Copy the code

To Read data from a text file, use the Read, ReadLine, or ReadAll methods of a TextStream object. The Read method reads a specified number of characters from a file; The ReadLine method reads an entire line, but does not include newlines; The ReadAll method reads the entire contents of the text file. The read content is stored in string variables for display and analysis. When reading the contents of a file using the Read or ReadLine methods, Skip or SkipLine methods are used if sections are skipped. The following code demonstrates opening the file, filling in the data, and then reading the data:

var fso, f1, ts, s;
var ForReading = 1;
fso = newActiveXObject (" Scripting. FileSystemObject ");// Create a fileF1 = fso. CreateTextFile (" c: \ \ testfile TXT ",true);
// Fill in a line of dataF1. WriteLine (" Hello World "); f1.WriteBlankLines(1);
// Close the file
f1.Close();
// Open the fileTs = fso. OpenTextFile (" c: \ \ testfile TXT ", ForReading);// Read a line from a file to a string
s = ts.ReadLine();
// Displays string informationAlert (" File contents = ' '+ s +' ");// Close the file
ts.Close();
Copy the code

(5) move, copy, and delete files For the above three kinds of File operations, javascript each have two corresponding methods: File. Move or FileSystemObject. MoveFile to Move files; File. Copy or FileSystemObject. Used by CopyFile used to Copy files; File. Delete or FileSystemObject. DeleteFile used to Delete the File. The following code shows how to create a text file in the root directory of drive C, fill in some content, then move the file to \ TMP, make a copy of the file under \temp, and finally delete the files in both directories:

var fso, f1, f2, s;
fso = newActiveXObject (" Scripting. FileSystemObject "); F1 = fso. CreateTextFile (" c: \ \ testfile TXT ",true);
/ / write a lineF1. Write (" This is a test. ");// Close the file
f1.Close();
// Get the handle to the file in C:\ rootF2 = fso. GetFile (" c: \ \ testfile TXT ");// Move the file to \ TMPF2. Move (" c: \ \ TMP \ \ testfile TXT ");// Copy files to \tempF2. Copy (" c: \ \ temp \ \ testfile TXT ");// Get the file handleF2 = fso. GetFile (" c: \ \ TMP \ \ testfile TXT "); F3 = fso. GetFile (" c: \ \ temp \ \ testfile TXT ");// Delete files
f2.Delete();
f3.Delete();
Copy the code

FileSystemObject, FileSystemObject, FileSystemObject, FileSystemObject, FileSystemObject, FileSystemObject, FileSystemObject, FileSystemObject However, the routines mentioned above are very simple, and it takes a lot of practice to fully and flexibly master javascript file manipulation techniques. It is also important to note that because of the advanced operations involved in reading and writing files in the browser, the default browser security level will always have a message before the code runs, which should be reminded of in the real world.