JavaScript system Folder file operation and parameter introduction _javascript tips

Source: Internet
Author: User
Early in the. NET for system operations, the same function to achieve a very complex, did not expect to use JavaScript is so simple, so search the Web code and improve.
Copy Code code as follows:

function PathList (path) {
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var Fldr = fso. GetFolder (path);
var fd = new Enumerator (Fldr. subfolders);
for (;!fd.atend (); Fd.movenext ()) {
SD = Fd.item ();
WScript.Echo (Sd.path);
Write to File
WriteFile ("A.text", Sd.path);
document.write (Sd.path);
PathList (Sd.path);
}
}
function FileList (path) {
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var Fldr = fso. GetFolder (path);
var fd = new Enumerator (Fldr. subfolders);
for (;!fd.atend (); Fd.movenext ()) {
SD = Fd.item ();
var fc = new Enumerator (sd.files);
for (;!fc.atend (); Fc.movenext ())
{
WScript.Echo (Fc.item ());
WriteFile ("B.text", Fc.item ());
}
WScript.Echo (Sd.path);
Write to File
WriteFile ("A.text", Sd.path);
document.write (Sd.path);
FileList (Sd.path);
}
}
Current directory file
function Curfilelist (path) {
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var Fldr = fso. GetFolder (path);
var fc = new Enumerator (fldr.files);
for (;!fc.atend (); Fc.movenext ())
{
WScript.Echo (Fc.item ());
WriteFile ("C.text", Fc.item ());
}
}
/*
Object. OpenTextFile (filename[, iomode[, create[, format]])
Parameters
Object
Required option. Object should be the name of the FileSystemObject.
FileName
Required option. A string expression that indicates the file you want to open.
IOMode
Options available. Can be one of three constants: ForReading, ForWriting, or forappending.
Create
Options available. Boolean value that indicates whether to create a new file when the specified filename does not exist. True If a new file is created, False if it is not created. If omitted, the new file is not created.
Format
Options available. Use one of the three-state values to indicate the format of the open file. If omitted, the file is opened in ASCII format.
Set up
The IOMode parameter can be any of the following settings:
Constant numerical description
ForReading 1 Opens the file as read-only. Can't write this file.
ForWriting 2 Open File as Write
ForAppending 8 Opens the file and begins writing at the end of the file.
The format parameter can be any of the following settings:
Value Description
TristateTrue opens the file in Unicode format.
Tristatefalse opens the file in ASCII format.
Tristateusedefault to open the file using the system default value.
*/
Read files
function readFile (filename) {
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var f = fso. OpenTextFile (filename,1);
var s = "";
while (!f.atendofstream)
S + + f.readline () + "\ n";
F.close ();
return s;
}
Write a file
function WriteFile (filename,filecontent) {
var fso, F, S;
FSO = new ActiveXObject ("Scripting.FileSystemObject");
f = fso. OpenTextFile (filename,8,true);
F.writeline (filecontent);
F.close ();
Alert (' OK ');
WScript.Echo ("Write Success");
}
deleting files
function DeleteFile (filename,filecontent) {
var fso, F, S;
FSO = new ActiveXObject ("Scripting.FileSystemObject");
f = fso. GetFile (filename);
F.delete ();
Alert (' OK ');
WScript.Echo ("Delete succeeded");
}
Bulk deletion, folder not deleted, no current directory file deleted
function Delfilelist (path) {
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var Fldr = fso. GetFolder (path);
var fd = new Enumerator (Fldr. subfolders);
for (;!fd.atend (); Fd.movenext ()) {
SD = Fd.item ();
var fc = new Enumerator (sd.files);
for (;!fc.atend (); Fc.movenext ())
{
WScript.Echo (Fc.item ());
WriteFile ("B.text", Fc.item ());
Fc.item (). Delete ();
WScript.Echo ("Delete succeeded");
}
WScript.Echo (Sd.path);
Write to File
WriteFile ("A.text", Sd.path);
WriteFile ("A.text", "delete Complete");
document.write (Sd.path);
Delfilelist (Sd.path);
}
}

Delete the current directory file
function Curdelfilelist (path) {
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var Fldr = fso. GetFolder (path);
var fc = new Enumerator (fldr.files);
for (;!fc.atend (); Fc.movenext ())
{
WScript.Echo (Fc.item ());
WriteFile ("C.text", Fc.item ());
Fc.item (). Delete ();
WriteFile ("C.text", "delete successful");
}
}
/*
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.
*/
function GetDriveInfo ()
{
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";
WScript.Echo (s);
}
Curfilelist ("D:\web_01");
FileList ("D:\web_01");
GetDriveInfo ();
Related Article

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.