On the Windows platform, js can call many ActivexObjects provided by Windows. This article uses js to implement document processing and gives a brief introduction to using js to write ActiveX.
The ActiveXObject object in JavaScript enables and returns a reference to the Automation object. How to use:
newObj = new ActiveXObject( servername.typename[, location])
ActiveXObject object syntax has these parts: newObj is required. The name of the variable to be assigned as ActiveXObject.
servername is required. The name of the application that provided this object.
typename is required. The type or class of object to create.
Location is optional. The name of the network server that created this object.
Remember: ActiveX is a Microsoft product, so this thing is only supported by IE!
Use ActiveXObject in javaScript to create FileSystemObject operation files
1. Function implementation core: FileSystemObject object
To implement file operation functions in JavaScript, we mainly rely on the FileSystemobject object.
2. FileSystemObject Programming
Programming using FileSystemObject objects is very simple. Generally, you need to go through the following steps: Create FileSystemObject objects, apply related methods, and access object-related properties.
(1) Create FileSystemObject object
The code to create a FileSystemObject object only takes one line:
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’s 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) Accessing object-related attributes
To access object-related attributes, you must first establish a handle to the object, which is achieved through the get series of methods: GetDrive is responsible for obtaining drive information, GetFolder is responsible for obtaining folder information, GetFile Responsible for obtaining 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 properties of the object. For example:
After executing the last sentence above, the last modified date attribute value of c:myjstest.txt will be displayed.
But please note one thing: for objects created using the create method, you no longer need to use the get method to obtain the object handle. In this case, you can directly use the handle name created by the create method:
3. Operating Drives
It is easy to use the FileSystemObject object to programmatically operate drives and folders, just like interacting with files in the Windows file browser, such as copying, moving folders, and obtaining folder information. property.
(1) Drives object attributes
The Drive object is responsible for collecting the physical or logical drive resource content in the system. It has the following attributes:
l TotalSize: The drive size calculated in bytes.
l AvailableSpace or FreeSpace: The available space of the drive calculated in bytes.
l DriveLetter: drive letter.
l DriveType: drive type, the value is: removable (removable media), fixed (fixed media), network (network resource), CD-ROM or RAM disk.
l SerialNumber: The serial number of the drive.
l FileSystem: The file system type of the drive, the values are FAT, FAT32 and NTFS.
l IsReady: Whether the drive is available.
l ShareName: Share name.
l VolumeName: Volume label name.
l Path and RootFolder: The path or root directory name of the drive.
(2) Drive object operation routines
The following routine displays information such as the volume label, total capacity and available space of drive C:
4. Operation folders (Folders)
Operations involving folders include creating, moving, deleting and obtaining related attributes.
Folder object operation routine:
The following routine will practice operations such as obtaining the name of the parent folder, creating a folder, deleting a folder, and determining whether it is the root directory:
5. Operation files (Files)
The operations on files are more complicated than the drive and folder operations introduced above. They are basically divided into the following two categories: file creation, copying, moving, deletion operations and file operations. Content creation, addition, deletion and read operations. Each is introduced in detail below.
(1) Create a file
There are 3 methods that can be used to create an empty text file. This kind of file is sometimes called a text stream.
The first is to use the CreateTextFile method. The code is as follows:
The second is to use the OpenTextFile method and add the ForWriting attribute. The value of ForWriting is 2. The code is as follows:
The third method is to use the OpenAsTextStream method, and also set the ForWriting attribute.
(2) Add data to file
After the file is created, you generally need to follow the steps of "Open the file -> Fill in the data -> Close the file" to add data to the file.
To open a file, use the OpenTextFile method of the FileSystemObject object, or the OpenAsTextStream method of the File object.
To fill in data, use the Write, WriteLine or WriteBlankLines method of the TextStream object. Under the same function of writing data, the difference between these three methods is that the Write method does not add a new line break at the end of the written data, the WriteLine method adds a new line break at the end, and WriteBlankLines adds one or more blanks. OK.
To close the file, you can use the Close method of the TextStream object.
(3) Routines for creating files and adding data
The following code combines the steps of creating a file, adding data, and closing the file:
(4) Read file content
To read data from a text file, use the Read, ReadLine or ReadAll method of the TextStream object. The Read method is used to read a specified number of characters in the file; the ReadLine method reads an entire line, excluding newlines; and the ReadAll method reads the entire content of the text file. The read content is stored in a string variable for display and analysis. When using the Read or ReadLine method to read the file content, if you want to skip some parts, you must use the Skip or SkipLine method.
The following code demonstrates opening a file, filling in data, and then reading the data:
(5) Moving, copying and deleting files
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 Used to delete files.
The following code demonstrates creating a text file in the root directory of drive C, filling in some content, then moving the file to the tmp directory, creating a file copy under the directory temp, and finally deleting the files in these two directories. :
6. Conclusion
Through the above introduction and examples of various objects, properties and methods of FileSystemObject, I believe you have a clear understanding of how to use JavaScript language to operate drives, files and folders on the page. However, the above-mentioned routines are very simple. To fully and flexibly master JavaScript file operation technology, a lot of practical exercises are required. And one more thing to remind everyone, because it involves advanced operations such as reading and writing files in the browser, for the default browser security level, there will be an information prompt before the code is run. Please check this in the actual environment. Prompt visitors to pay attention.