Javascript system folder File Operations

Source: Internet
Author: User

In the early stage, system operations were performed under. net, and the implementation of the same functions was very complicated. I didn't expect javascript to be so simple, so I searched the code online and improved it.

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 a 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 a 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. The object should be the name of FileSystemObject.

Filename

Required. Specifies the string expression of the file to be opened.

Iomode

Optional. It can be one of the three constants: ForReading, ForWriting, or ForAppending.

Create

Optional. Boolean value, indicating whether to create a new file when the specified filename does not exist. If a new file is created, the value is True. If the file is not created, the value is False. If not, no new file is created.

Format

Optional. Use one of the three-state values to specify the file opening format. If ignored, the file is opened in ASCII format.

Set

The iomode parameter can be any of the following settings:

Constant Value description

ForReading 1 open the file in read-only mode. This file cannot be written.

ForWriting 2 open the file in write mode

ForAppending 8 open the file and write it from the end of the file.

The format parameter can be any of the following settings:

Value description

TristateTrue open a file in Unicode format.

TristateFalse open an object in ASCII format.

TristateUseDefault: open a file by default.

*/

// Read the file

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 an object

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 ("successfully written ");

}

// Delete an object

Function deleteFile (filename, filecontent ){

Var fso, f, s;

Fso = new ActiveXObject ("Scripting. FileSystemObject ");

F = fso. GetFile (filename );

F. Delete ();

// Alert ('OK ');

WScript. Echo ("deleted successfully ");

}

// Batch Delete. The folder is not deleted and the current directory file cannot be 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 ("deleted successfully ");

}

WScript. Echo (sd. path );

// Write a file

WriteFile ("a. text", sd. path );

WriteFile ("a. text", "deleted ");

// 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", "deleted successfully ");

}

}

/*

The Drive object collects the content of physical or logical Drive resources in the system. It has the following attributes:

L TotalSize: the size of the drive in bytes.

L AvailableSpace or FreeSpace: The drive space in bytes.

L DriveLetter: drive letter.

L DriveType: Drive Type, value: removable (Mobile Medium), fixed (fixed medium), network (network Resources), CD-ROM or ramdisk.

L SerialNumber: the serial number of the drive.

L FileSystem: The file system type of the drive. Values: FAT, FAT32, and NTFS.

L IsReady: whether the drive is available.

L ShareName: Share Name.

L VolumeName: the name of the volume label.

L Path and RootFolder: The drive Path or root directory name.

*/

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.