Home > Web Front-end > JS Tutorial > How to use ActiveXObject to operate local folders in JavaScript_javascript skills

How to use ActiveXObject to operate local folders in JavaScript_javascript skills

WBOY
Release: 2016-05-16 16:54:00
Original
969 people have browsed it

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.

Copy code The code is as follows:











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:

.

Copy code The code is as follows:

var fso = new ActiveXObject("Scripting.FileSystemObject") ;
var f1 = fso.GetFile("c:\myjstest.txt");
alert("File last modified: " f1.DateLastModified);

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:

Copy code The code is as follows:

var fso = new ActiveXObject("Scripting.FileSystemObject") ;
var f1 = fso.createtextfile("c:\myjstest.txt",true");
alert("File last modified: " f1.DateLastModified);

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:

Copy the code The code is as follows :

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. 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:

Copy code The code is as follows:

var fso, fldr, s = "";
// Create FileSystemObject object instance
fso = new ActiveXObject("Scripting.FileSystemObject");
// Get Drive object
fldr = fso.GetFolder("c:\");
/ / Display the name of the parent directory
alert("Parent folder name is: " fldr "n");
// Display the name of the drive
alert("Contained on drive " fldr.Drive "n");
// Determine whether it is the root directory
if (fldr.IsRootFolder)
alert("This is the root folder.");
else
alert("This folder isn't a root folder.");
alert("nn");
// Create a new folder
fso.CreateFolder ("C:\Bogus");
alert("Created folder C: \Bogus" "n");
//Display the folder base 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. 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:

Copy code 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 the ForWriting attribute. The value of ForWriting is 2. The code is as follows:

Copy code 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 also set the ForWriting attribute.

Copy code 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 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:

Copy code The code is as follows:

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 newline characters
tf.WriteLine("Testing 1, 2, 3.") ;
// Add 3 blank lines
tf.WriteBlankLines(3) ;
// Fill in one line without line breaks
tf.Write ("This is a test.");
//Close the file
tf.Close();

(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:

Copy code The code is as follows:

var fso, f1, ts, s;
var ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
// Create file
f1 = fso.CreateTextFile("c:\testfile.txt", true);
// Fill in a line of data
f1.WriteLine("Hello World");
f1.WriteBlankLines(1);
// Close the file
f1.Close();
// Open the file
ts = fso.OpenTextFile("c:\testfile.txt", ForReading);
// Read one line of the file into a 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 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. :

Copy code The code is as follows:

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.");
// Close the file
f1.Close();
// Get the file handle in the C: root directory
f2 = fso.GetFile("c:\ testfile.txt");
// Move the file to the mp directory
f2.Move ("c:\tmp\testfile.txt");
// Copy the file to the emp 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 file
f2.Delete();
f3.Delete();

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template