FileSystemObject handling files

xiaoxiao2021-03-06  98

There are two main file processing types:

Create, add, or delete data, and read files

Move, copy and delete files

Create a file

Creating an empty text file (sometimes called "text stream") has three ways.

The first method is to use the CreateTextFile method. The following example demonstrates how to create text files in this method in VBScript:

DIM FSO, F1

SET FSO = CreateObject ("scripting.filesystemObject")

Set f1 = fso.createtextFile ("c: /testfile.txt", true)

To use this method in JScript, use the following code:

VAR FSO, F1;

Fso = New ActiveXObject ("scripting.filesystemObject");

F1 = fso.createtextfile ("c: //testfile.txt", true);

Please check the sample code to understand how to use the CreateTextFile method in FileSystemObject.

The second method of creating a text file is to use the OpenTextFile method of the FILESystemObject object and set the Forwriting flag. In VBScript, the code is like the example below:

Dim Fso, TS

Const forwriting = 2

Set fso = creteObject ("scripting. FilesystemObject")

Set ts = fso.opentextfile ("c: /test.txt", forwriting, true)

To use this method in JScript to create a text file, use the following code:

VAR FSO, TS;

Var forwriting = 2;

Fso = New ActiveXObject ("scripting.filesystemObject");

Ts = fso.opentextfile ("c: //test.txt", forwriting, true);

The third method for creating a text file is to use the OpenASTextStream method and set the Forwriting flag. To use this method, use the following code in VBScript:

DIM FSO, F1, TS

Const forwriting = 2

SET FSO = CreateObject ("scripting.filesystemObject")

Fso.createtextfile ("c: /test1.txt")

Set f1 = fso.getfile ("c: /test1.txt")

SET TS = f1.openastextStream (ForWriting, True)

In JScript, use the code in the example below:

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); Add data to the file

Once a text file is created, add data to the file using the following three steps:

Open the text file.

data input.

Close the file.

To open an existing file, use the OpenTextFile method of the FileSystemObject object or the OpenASTextStream method for the File object.

To write data to open text files, the Write, WriteLine or Writeblanklines method of the TextStream object is used under the following table.

Task method

Write data to the open text file without subsequent a new row character. Write

Write data to the open text file, follow, a new line character. WriteLine

Write one or more blank lines to the open text file. Writeblanklines

Please examine sample code to understand how Write, WriteLine, and Writeblanklines methods are used in the FileSystemObject object.

To turn off an open file, use the Close method of the TextStream object.

Please examine sample code to understand how to use a Close method in FileSystemObject.

-------------------------------------------------- ------------------------------

Note that the new row character contains one or more characters (depending on the operating system) to move the cursor to the start position of the next row (Enter / Return). Note that some string may already have this non-printing character.

-------------------------------------------------- ------------------------------

The following VBScript example demonstrates how to open the file, and simultaneously use three write methods to add data to the file, then close the file:

SUB CREATEFILE ()

DIM FSO, TF

SET FSO = CreateObject ("scripting.filesystemObject")

Set tf = fso.createtextFile ("c: /testfile.txt", true)

'Write a row and have a new line character.

Tf.Writeline ("Testing 1, 2, 3.")

'Write three new line characters to the file.

Tf.writeblanklines (3)

'Write a row.

Tf.write ("this is a test.")

Tf.close

End Sub

This example demonstrates how to use these three methods in JScript:

Function cretefile ()

{

VAR FSO, TF;

Fso = New ActiveXObject ("scripting.filesystemObject");

Tf = fso.createtextfile ("c: //testfile.txt", true);

// Write a row and have a new line character.

Tf.writeline ("Testing 1, 2, 3.");

/ / Write three new line characters to the file.

Tf.writeblanklines (3);

// Write a line.

Tf.write ("this is a test.");

Tf.close ();

}

Read file

To read data from a text file, use the Read, Readline or Readall method of the TextStream object. The following table describes which method should be used for different tasks. Task method

Read the specified number of characters from the file. Reta

Read a whole (until but does not include new line characters). Readline

Read the entire content of the text file. Readall

Please examine sample code to understand how to use Readall and Readline methods in FileSystemObject.

If you use the READ or READLINE method, and you want to skip the special parts of the data, use the SKIP or SKIPLINE method. The result text of the read method has a string that can be displayed in one control or to analyze, connect, and so on.

The following VBScript example demonstrates how to open a file, and how to write data into the file and read data from the file:

Sub Readfiles

DIM FSO, F1, TS, S

Const forreading = 1

SET FSO = CreateObject ("scripting.filesystemObject")

Set f1 = fso.createtextFile ("c: /testfile.txt", true)

'Write a row.

Response.write "Writing file

"

F1.writeLine "Hello World"

f1.writeblanklines (1)

f1.close

'Read the contents of the file.

Response.write "Reading File

"

Set ts = fso.opentextfile ("c: /testfile.txt", forreading)

s = ts.readline

Response.write "File Contents = '" & S & "" "

Ts.close

End Sub

The following code demonstrates the same thing in JScript:

Function readfiles ()

{

VAR FSO, F1, TS, S;

Var forreading = 1;

Fso = New ActiveXObject ("scripting.filesystemObject");

F1 = fso.createtextfile ("c: //testfile.txt", true);

// Write a line.

Response.write ("Writing file

");

F1.writeline ("Hello World");

f1.writeblanklines (1);

f1.close ();

// read the contents of the file.

Response.write ("Reading File

");

Ts = fso.opentextfile ("c: //testfile.txt", forreading);

S = ts.readline ();

Response.write ("File Contents = '" S "");

Ts.close ();

}

Move, copy and delete files

FSO object mode has two ways to move, copy, and delete files as described in the table below.

Task method

Mobile file file.move or filesystemObject.movefile copy file file.copy or FileSystemObject.copyfile

Delete file file.delete or FileSystemObject.deletefile

Please examine sample code to take two methods of removing files in FileSystemObject.

The following VBScript example creates a text file in the root of the drive C, writes some information, then moves it to the / TMP directory, and makes a backup in / temp, and finally put them from two directories Delete.

To run the following example, you need to create a / TMP and / TEMP directory in the root of the drive C:

Submanipfiles

DIM FSO, F1, F2, S

SET FSO = CreateObject ("scripting.filesystemObject")

Set f1 = fso.createtextFile ("c: /testfile.txt", true)

Response.write "Writing file

"

'Write a row.

F1.write ("this is a test.")

'Close the file.

f1.close

Response.write "Moving File to C: / TMP

"

'Get the handle of the file in the root directory (C: /).

Set f2 = fso.getfile ("c: /testfile.txt")

'Move the file to the / TMP directory.

F2.move ("c: /tmp/testfile.txt")

Response.write "Copying File to C: / Temp

"

'Copy the file to the / temp directory.

f2.copy ("c: /temp/testfile.txt")

Response.write "Deleting Files

"

'Get the handle of the current location of the file.

Set f2 = fso.getfile ("c: /tmp/testfile.txt")

Set f3 = fso.getfile ("c: /temp/testfile.txt")

' Delete Files.

F2.delete

f3.delete

Response.write "All Done!"

End Sub

The following code demonstrates the same thing in JScript:

Function manipfiles ()

{

VAR FSO, F1, F2, S;

Fso = New ActiveXObject ("scripting.filesystemObject");

F1 = fso.createtextfile ("c: //testfile.txt", true);

Response.write ("Writing file

");

// Write a line.

F1.write ("this is a test.");

// Turn the file.

f1.close ();

Response.write ("Moving File to C: // TMP

");

/ / Get the handle of the file in the root directory (C: /).

F2 = fso.getfile ("c: //testfile.txt");

// Move the file to the / TMP directory.

F2.move ("c: //tmp/testfile.txt");

Response.write ("Copying File To C: // Temp");

// Copy the file to the / temp directory.

F2.copy ("c: //temp/testfile.txt");

Response.write ("DELETING FILES)

");

// Get the handle of the current location of the file.

F2 = fso.getfile ("c: //tmp/testfile.txt");

F3 = fso.getfile ("c: //temp/testfile.txt");

// Delete Files.

f2.delete ();

f3.delete ();

Response.write ("all done!");

}

转载请注明原文地址:https://www.9cbs.com/read-123706.html

New Post(0)