FileSystemObject has two main types of file processing:
Create, add, or delete data, and read files
Moving, copying, and deleting files
Create a file
There are three ways to create an empty text file (sometimes called a "text stream").
The first method is to use the CreateTextFile method. The following example demonstrates how to create a text file in VBScript in this way:
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);
Examine the sample code to see how to use the CreateTextFile method in FileSystemObject.
The second way to create 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 following example:
Dim FSO, TS
Const ForWriting = 2
Set fso = CreateObject ("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 way to create 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 following example:
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 a file
Once you have created a text file, use the following three steps to add data to the file:
Open a text file.
Write Data.
Closes the file.
To open an existing file, use the OpenTextFile method of the FileSystemObject object or the OpenAsTextStream method of the file object.
To write data to an open text file, use the Write, WriteLine, or WriteBlankLines method of the TextStream object according to the tasks described in the following table.
Task methods
Writes data to an open text file without following a new line character. Write
Writes data to an open text file, followed by a new line character. WriteLine
Writes one or more blank lines to an open text file. WriteBlankLines
Examine the sample code to see how to use the Write, WriteLine, and WriteBlankLines methods in the FileSystemObject object.
To turn off an open file, use the Close method of the TextStream object.
Examine the sample code to see how to use the Close method in FileSystemObject.
--------------------------------------------------------------------------------
Note that the new line character contains one or more characters (depending on the operating system) to move the cursor to the beginning of the next line (carriage return/linefeed). Note that some strings may already have this nonprinting character at the end.
--------------------------------------------------------------------------------
The following VBScript example shows how to open a file and use three writing methods to add data to a file, and then close the file:
Sub CreateFile ()
Dim FSO, TF
Set fso = CreateObject ("Scripting.FileSystemObject")
Set tf = fso. CreateTextFile ("C:\testfile.txt", True)
' Writes a line with 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
End Sub
This example demonstrates how to use these three methods in JScript:
function CreateFile ()
{
var fso, TF;
FSO = new ActiveXObject ("Scripting.FileSystemObject");
tf = FSO. CreateTextFile ("C:\\testfile.txt", true);
Writes a line with a new line character.
Tf. WriteLine ("Testing 1, 2, 3.");
Writes three new line characters to a file.
Tf. WriteBlankLines (3);
Write a line.
Tf. Write ("This is a test.");
Tf. Close ();
}
Reading files
To read data from a text file, use the read, ReadLine, or ReadAll method of the TextStream object. The following table describes which methods should be used for different tasks.
Task methods
Reads a specified number of characters from a file. Read
Reads an entire line (all the time but does not include new line characters). ReadLine
Reads the entire contents of a text file. ReadAll
Examine the sample code to see how to use the ReadAll and ReadLine methods in FileSystemObject.
If you use the Read or ReadLine method and you want to skip a particular part of the data, use the Skip or SkipLine method. The result text of the Read method exists in a string that can be displayed in a control, or it can be parsed, connected, and so on by string functions such as left, right, and Mid.
The following VBScript example demonstrates how to open a file and how to write data to a file and read data from a 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 line.
Response.Write "Writing File <br>"
F1. WriteLine "Hello World"
F1. WriteBlankLines (1)
F1. Close
' Read the contents of the file.
Response.Write "Reading File <br>"
Set ts = fso. OpenTextFile ("C:\testfile.txt", ForReading)
s = ts. ReadLine
Response.Write "File contents = '" & S & "'"
Ts. Close
End Sub