JS gets the list of files for the local folder

Source: Internet
Author: User
Tags file system resource access root directory

First, the function realizes the core: FileSystemObject object
To implement the file manipulation function in JavaScript, the main thing is to rely on the FileSystemObject object.
Second, FileSystemObject programming
Programming with a FileSystemObject object is simple, typically through the following steps: Creating FileSystemObject objects, applying related methods, and accessing object-related properties.
(i) Creating FileSystemObject objects
That creates the FileSystemObject object.
The code takes only 1 lines:
var fso = new ActiveXObject ("Scripting.FileSystemObject");
After the above code executes, the FSO becomes a FileSystemObject object instance.
(ii) Application of relevant methodologies
After you create an object instance, you can use the associated methods of the object. 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) Access to object-related properties
To access the related properties of an object, you first establish a handle to the object, which is done by 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, after pointing to the following code, F1 becomes a handle 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 associated 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 the last sentence is executed, the last modified Date attribute value for C:\myjstest.txt is displayed.
But one thing. Note: For objects created using the Create method, you do not have to use the Get method to obtain the object handle, and then the handle name created directly by using the Create method can be:
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var f1 = fso.createtextfile ("C:\\myjstest.txt", true);
Alert ("File Last modified:" + F1.) DateLastModified);
Third, the operation of the driver (drives)
Using the FileSystemObject object to programmatically manipulate drives (drives) and folders (Folders) is easy, like interacting with files in a Windows file browser, such as copying, moving folders, and getting the properties of a folder.
(i) Drives object properties
The drive object is responsible for collecting the contents of the physical or logical drive resource in the system, which has the following properties:
L TotalSize: The drive size in bytes (byte).
L availablespace or FreeSpace: drive free space calculated in bytes (byte).
L DriveLetter: drive letter.
L DriveType: Drive type, value is: removable (mobile media), fixed (fixed media), Network (network Resource), CD-ROM, or RAM disk.
L SerialNumber: Serial code for the drive.
L FileSystem: The file system type of the drive on which the value is fat, FAT32, and NTFS.
L IsReady: The drive is available.
L ShareName: share name.
L VolumeName: Volume label name.
L Path and RootFolder: The path of the drive or the name of the root directory.
(ii) Drive object manipulation routines
The following routines show information such as the volume label, Total capacity, and free space of drive C:
var fso, DRV, s = "";
FSO = new ActiveXObject ("Scripting.FileSystemObject");
DRV = FSO. Getdrive (FSO. GetDriveName ("c:\\"));
S + + "Drive C:" + "-";
S + + DRV. VolumeName + "\ n";

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.