JS, ActiveXObject, Scripting.FileSystemObject

Source: Internet
Author: User
Tags install window

JavaScript file Manipulation (IE)

First, the function realizes the core: FileSystemObject object
To implement the file operation function in JavaScript, the main thing is to rely on the FileSystemObject object.
Second, FileSystemObject programming
Programming with the FileSystemObject object is simple and generally takes the following steps: Create a FileSystemObject object, apply a related method, Access object-related properties.
(i) Creating FileSystemObject objects
That creates the FileSystemObject object.
The code is just 1 lines:
var fso = new ActiveXObject ("Scripting.FileSystemObject");
Once the above code is executed, the FSO becomes an FileSystemObject object instance.
(ii) Application of relevant methodologies
Once you have created 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 ");
(iii) Accessing object-related properties
To access the related properties of an object, first establish a handle to the object, which is achieved through the Get series method: Getdrive is responsible for obtaining the drive information, GetFolder is responsible for obtaining the folder information, GetFile is responsible for obtaining the file information. For example, when you point to the following code, F1 becomes a handle to the file C:est.txt:
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var f1 = fso. GetFile ("C:\myjstest.txt");
Then, use F1 to access the related properties of the object. Like what:
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 property value for C:myjstest.txt is displayed.
But one thing to note: For objects created using the Create method, you no longer have to use the Get method to get an object handle, and the name of a handle created directly using the Create method can:
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var f1 = fso.createtextfile ("C:\myjstest.txt", true ");
Alert ("File Last modified:" + F1. DateLastModified);
Third, Operation Driver (Drives)
It is easy to programmatically manipulate drives (Drives) and folders (Folders) using the FileSystemObject object, which is like interacting with files in the Windows file browser, such as copying, moving folders, and getting the properties of a folder.
(i) Drives object properties
The drive object collects the contents of the physical or logical drives resource in the system, which has the following properties:
L TotalSize: The drive size is calculated in bytes (byte).
L AvailableSpace or FreeSpace: The amount of free space that is calculated for the drive in bytes (byte).
L DriveLetter: drive letter.
L DriveType: Drive type, Value: Removable (mobile media), fixed (stationary media), Network (Networking Resource), CD-ROM or RAM disk.
L SerialNumber: Serial Code of the drive.
L FileSystem: The file system type of the drive on which the value is 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 to the drive or the root directory name.
(ii) Drive object operation routines
The following routines show information such as volume label, Total capacity, and free space for drive C:
var fso, DRV, s = "";
FSO = new ActiveXObject ("Scripting.FileSystemObject");
DRV = FSO. Getdrive (FSO. GetDriveName ("C: \"));
s + = "Drive C:" + "-";
s + = DRV. VolumeName + "";
s + = "Total Space:" + DRV. totalsize/1024;
s + = "Kb" + "";
s + = "free Space:" + DRV. freespace/1024;
s + = "Kb" + "";
alert (s);
Iv. operating folder (Folders)
Actions that involve folders include creating, moving, deleting, and getting related properties.
Folder object Operation routines:
The following routines practice getting the parent folder name, creating a folder, deleting a folder, and judging whether it is the root directory, and so on:
var fso, fldr, S = "";
Creating an FileSystemObject object instance
FSO = new ActiveXObject ("Scripting.FileSystemObject");
Get the Drive object
Fldr = fso. GetFolder ("C: \");
Show Parent directory Name
Alert ("Parent folder name is:" + Fldr + "");
Show Drive name
Alert ("Contained on drive" + Fldr. Drive + "");
Determine if the root directory
if (Fldr. IsRootFolder)
Alert ("This is the root folder.");
Else
Alert ("This folder isn ' t a root folder.");
Alert ("");
Create a new Folder
Fso. CreateFolder ("C:\Bogus");
Alert ("Created folder C:\Bogus" + "");
Show folder base name, without path name
Alert ("Basename =" + FSO.) Getbasename ("C:\bogus") + "");
Delete the Created folder
Fso. DeleteFolder ("C:\Bogus");
Alert ("Deleted folder C:\Bogus" + "");
V. Operational documents (FILES)
The operation of the file is more complex than the drive and folder operations described above, and is basically divided into the following two categories: Create, copy, move, delete, and create, add, delete, and read file contents. Described in detail below.
(i) Create a file
There are 3 ways to create an empty text file, which is sometimes called a text stream.
The first is the use of 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 the upper ForWriting property, with a value of ForWriting of 2. 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 is to use the OpenAsTextStream method, as well as set the ForWriting property. 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);
(ii) Adding data to a file
When a file is created, it is generally required to add data to a file by following the steps "Open file-fill data, close file".
Open the file to use the OpenTextFile method of the FileSystemObject object, or use the OpenAsTextStream method of the file object.
Fill in the data to use the Write, WriteLine, or WriteBlankLines methods of the TextStream object. In the same way that the write data is implemented, the difference between the 3 is that the Write method does not add new line breaks at the end of the write data, the WriteLine method adds a new line break at the end, and WriteBlankLines adds one or more empty rows.
Close the file can use the Close method of the TextStream object.
(iii) creating files and adding data routines
The following code combines several steps to create a file, add data, and close a file to apply:
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 line breaks
Tf. WriteLine ("Testing 1, 2, 3.");
Add 3 Blank Lines
Tf. WriteBlankLines (3);
Fill in one line with no line break
Tf. Write ("This is a test.");
Close File
Tf. Close ();
(d) Read the contents of the file
Read data from a text file to use the read, ReadLine, or ReadAll methods of the TextStream object. The Read method is used to read a specified number of characters in a file; the ReadLine method reads a whole line, but does not include a newline character; the ReadAll method reads the entire contents of the text file. The contents of the read are stored in a string variable for display and analysis.

Method or Property Description
BuildPath ()
Generate a file path
CopyFile () Copying files
CopyFolder () Copy directory
CreateFolder () Create a new directory
CreateTextFile () Generate a file
DeleteFile () Delete a file
DeleteFolder () Delete a directory
Driveexists () Verify that the drive letter is present
Drives returns a collection of drive characters
FileExists () Verify that the file exists
FolderExists checking if a directory exists
Getabsolutepathname () get an absolute path to a file
Getbasename () Get file name
Getdrive () get the drive letter name
GetDriveName () get the drive letter name
Getextensionname () Get the suffix of the file
GetFile () Generate File Object
GetFileName () Get file name
GetFolder () Get directory Object
Getparentfoldername gets the parent directory name of the file or directory
GetSpecialFolder () Get a special directory name
GetTempName () generates a temporary file object
MoveFile () Moving files
MoveFolder () Move directory
OpenTextFile () open a file stream
F.files//directory of all file collections
F.attributes//File properties
Case 0 str= "normal file. No properties are set. "
Case 1 Str= "read-only file. can read and write. "
Case 2 str= "hidden file. can read and write. "
Case 4 str= "System file. can read and write. "
Case str= "folder or directory. Read-only. "
Case str= "files that have changed since the last backup." can read and write. "
Case 1024x768 str= "link or shortcut. Read-only. "
Case 2048 str= "compressed file. Read-only. "
f.datecreated//Creation time
f.datelastaccessed//Last access time
F.datelastmodified//Last Modified
F.path//file path
F.name//File name
F.type//File type
F.size//File Size (units: bytes)
F.parentfolder//Parent Directory
F.rootfolder//root directory
Example description
BuildPath (path, file name)//This method adds a file to the given path and automatically adds a delimiter

CopyFile (source file, target file, overwrite)//copy source file to target file, when overwrite value is true, overwrite file if destination file exists

CopyFolder (object directory, target directory, overwrite)//Copy the object directory to the target directory, when overwrite is true, if the target directory exists will overwrite the file

CreateFolder (directory name)//Create a new directory

CreateTextFile (file name, overwrite)//Create a new file, if this file already exists, you need to set the override value to True

DeleteFile (file name, read-only?) )//Delete a file, if the file's properties are read-only, you need to set the read-only value to True

DeleteFolder (file name, read-only?) //Delete a directory, if the directory's properties are read-only, you need to set the read-only value to True

Driveexists (drive letter)//check whether a disk exists, if it exists, return to true, does not exist on the back ....

FileExists (file name)//Check whether a file exists, if it exists, return to true, does not exist ....

FolderExists (directory name)//check whether a directory exists, if it exists, it will be true, does not exist to return ....

Getabsolutepathname (File object)//returns the absolute path of the file object in the system

Getbasename (File object)//Return file name of the object

Getextensionname (File object)//suffix of file

Getparentfoldername (File object)//Get directory name of Parent

GetSpecialFolder (Directory code)//Get some special directories in the system path, directory code 3 is 0: Install window Directory 1: System files directory 2: Temp file directory

GetTempName ()///Generate a random temporary file object, followed by a number of random numbers in the RAD lead, as if some software is generated during installation *.tmp

MoveFile (source file, destination file)//move the source file to the location of the destination file

JS, ActiveXObject, Scripting.FileSystemObject

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.