There are two main file processing types:
Create, add or delete data, and read files
Move, copy and delete 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 shows how to create a text file in VBScript:
Dim fso, f1
Set fso = CreateObject("")
Set f1 = ("c:\", True)
To use this method in JScript, use the following code:
var fso, f1;
fso = new ActiveXObject("");
f1 = ("c:\\", true);
Please examine the sample code to understand 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 = ("c:\", ForWriting, True)
To use this method to create a text file in JScript, use the following code:
var fso, ts;
var ForWriting= 2;
fso = new ActiveXObject("");
ts = ("c:\\", 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("")
("c:\")
Set f1 = ("c:\")
Set ts = (ForWriting, True)
In JScript, use the code from the following example:
var fso, f1, ts;
var ForWriting = 2;
fso = new ActiveXObject("");
("c:\\");
f1 = ("c:\\");
ts = (ForWriting, true);
Add data to the file
Once the text file is created, use the following three steps to add data to the file:
Open the text file.
Write data.
Close 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 methods of the TextStream object according to the tasks described in the following table.
Task Method
Write data to the open text file without the need for a new line character. Write
Write data to the open text file, followed by a new line character. WriteLine
Write one or more blank lines to the open text file. WriteBlankLines
Please examine the sample code to understand how to use the Write, WriteLine, and WriteBlankLines methods in FileSystemObject objects.
To close an open file, use the Close method of the TextStream object.
Please examine the sample code to understand how to use the Close method in FileSystemObject.
--------------------------------------------------------------------------------
Note The new line character contains one or several characters (depending on the operating system) to move the cursor to the start position of the next line (Carriage Enter/Line Win). Note that some strings may already have this non-printed character at the end.
--------------------------------------------------------------------------------
The following VBScript example shows how to open a file, and use three writing methods to add data to the file and then close the file:
Sub CreateFile()
Dim fso, tf
Set fso = CreateObject("")
Set tf = ("c:\", True)
' Write a line with a new line character.
("Testing 1, 2, 3.")
' Write three new line characters to the file.
(3)
' Write a line.
("This is a test.")
End Sub
This example demonstrates how to use these three methods in JScript:
function CreateFile()
{
var fso, tf;
fso = new ActiveXObject("");
tf = ("c:\\", true);
// Write a line with new line characters.
("Testing 1, 2, 3.") ;
// Write three new line characters to the file.
(3) ;
// Write a line.
("This is a test.");
();
}
Read the file
To read data from a text file, use the Read, ReadLine, or ReadAll methods of the TextStream object. The following table describes which method should be used for different tasks.
Task Method
Read a specified number of characters from the file. Read
Read an entire line (all the way to but not include new line characters). ReadLine
Read the entire contents of the text file. ReadAll
Please check the sample code to understand how to use the ReadAll and ReadLine methods in FileSystemObject.
If you use the Read or ReadLine method and want to skip special parts of the data, use the Skip or SkipLine method. The result text of the read method is present in a string, which can be displayed in a control, or can be analyzed using string functions (such as Left, Right, and Mid) to connect, etc.
The following VBScript example demonstrates how to open a file, and how to write data into and read data from the file:
Sub ReadFiles
Dim fso, f1, ts, s
Const ForReading = 1
Set fso = CreateObject("")
Set f1 = ("c:\", True)
' Write a line.
"Writing file <br>"
"Hello World"
(1)
' Read the contents of the file.
"Reading file <br>"
Set ts = ("c:\", ForReading)
s =
"File contents = '" & s & "'"
End Sub
The following code demonstrates doing the same thing in JScript:
function ReadFiles()
{
var fso, f1, ts, s;
var ForReading = 1;
fso = new ActiveXObject("");
f1 = ("c:\\", true);
// Write a line.
("Writing file <br>");
("Hello World");
(1);
();
// Read the contents of the file.
("Reading file <br>");
ts = ("c:\\", ForReading);
s = ();
("File contents = '" + s + "'");
();
}
Move, copy and delete files
Each FSO object mode has two methods to move, copy and delete files, as described in the following table.
Task Method
Move file or
Copy the file or
Delete a file or
Please examine the sample code to understand two ways to delete files in FileSystemObject.
In the following VBScript example, create a text file in the root directory of drive C, write some information to it, then move it to the \tmp directory, and make a backup in \temp, and finally delete them from both directories.
To run the following example, you need to first create the \tmp and \temp directories in the root directory of drive C:
Sub ManipFiles
Dim fso, f1, f2, s
Set fso = CreateObject("")
Set f1 = ("c:\", True)
"Writing file <br>"
' Write a line.
("This is a test.")
' Close the file.
"Moving file to c:\tmp <br>"
' Get the handle of the file in the root directory of C (C:\).
Set f2 = ("c:\")
' Move the file to the \tmp directory.
("c:\tmp\")
"Copying file to c:\temp <br>"
' Copy the file to the \temp directory.
("c:\temp\")
"Deleting files <br>"
' Get the handle to the current location of the file.
Set f2 = ("c:\tmp\")
Set f3 = ("c:\temp\")
' Delete the file.
"All done!"
End Sub
The following code demonstrates doing the same thing in JScript:
function ManipFiles()
{
var fso, f1, f2, s;
fso = new ActiveXObject("");
f1 = ("c:\\", true);
("Writing file <br>");
// Write a line.
("This is a test.");
// Close the file.
();
("Moving file to c:\\tmp <br>");
// Get the handle of the file in the root directory of C (C:\).
f2 = ("c:\\");
// Move the file to the \tmp directory.
("c:\\tmp\\");
("Copying file to c:\\temp <br>");
// Copy the file to the \temp directory.
("c:\\temp\\");
("Deleting files <br>");
// Get the handle to the current location of the file.
f2 = ("c:\\tmp\\");
f3 = ("c:\\temp\\");
// Delete the file.
();
();
("All done!");
}
Create, add or delete data, and read files
Move, copy and delete 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 shows how to create a text file in VBScript:
Dim fso, f1
Set fso = CreateObject("")
Set f1 = ("c:\", True)
To use this method in JScript, use the following code:
var fso, f1;
fso = new ActiveXObject("");
f1 = ("c:\\", true);
Please examine the sample code to understand 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 = ("c:\", ForWriting, True)
To use this method to create a text file in JScript, use the following code:
var fso, ts;
var ForWriting= 2;
fso = new ActiveXObject("");
ts = ("c:\\", 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("")
("c:\")
Set f1 = ("c:\")
Set ts = (ForWriting, True)
In JScript, use the code from the following example:
var fso, f1, ts;
var ForWriting = 2;
fso = new ActiveXObject("");
("c:\\");
f1 = ("c:\\");
ts = (ForWriting, true);
Add data to the file
Once the text file is created, use the following three steps to add data to the file:
Open the text file.
Write data.
Close 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 methods of the TextStream object according to the tasks described in the following table.
Task Method
Write data to the open text file without the need for a new line character. Write
Write data to the open text file, followed by a new line character. WriteLine
Write one or more blank lines to the open text file. WriteBlankLines
Please examine the sample code to understand how to use the Write, WriteLine, and WriteBlankLines methods in FileSystemObject objects.
To close an open file, use the Close method of the TextStream object.
Please examine the sample code to understand how to use the Close method in FileSystemObject.
--------------------------------------------------------------------------------
Note The new line character contains one or several characters (depending on the operating system) to move the cursor to the start position of the next line (Carriage Enter/Line Win). Note that some strings may already have this non-printed character at the end.
--------------------------------------------------------------------------------
The following VBScript example shows how to open a file, and use three writing methods to add data to the file and then close the file:
Sub CreateFile()
Dim fso, tf
Set fso = CreateObject("")
Set tf = ("c:\", True)
' Write a line with a new line character.
("Testing 1, 2, 3.")
' Write three new line characters to the file.
(3)
' Write a line.
("This is a test.")
End Sub
This example demonstrates how to use these three methods in JScript:
function CreateFile()
{
var fso, tf;
fso = new ActiveXObject("");
tf = ("c:\\", true);
// Write a line with new line characters.
("Testing 1, 2, 3.") ;
// Write three new line characters to the file.
(3) ;
// Write a line.
("This is a test.");
();
}
Read the file
To read data from a text file, use the Read, ReadLine, or ReadAll methods of the TextStream object. The following table describes which method should be used for different tasks.
Task Method
Read a specified number of characters from the file. Read
Read an entire line (all the way to but not include new line characters). ReadLine
Read the entire contents of the text file. ReadAll
Please check the sample code to understand how to use the ReadAll and ReadLine methods in FileSystemObject.
If you use the Read or ReadLine method and want to skip special parts of the data, use the Skip or SkipLine method. The result text of the read method is present in a string, which can be displayed in a control, or can be analyzed using string functions (such as Left, Right, and Mid) to connect, etc.
The following VBScript example demonstrates how to open a file, and how to write data into and read data from the file:
Sub ReadFiles
Dim fso, f1, ts, s
Const ForReading = 1
Set fso = CreateObject("")
Set f1 = ("c:\", True)
' Write a line.
"Writing file <br>"
"Hello World"
(1)
' Read the contents of the file.
"Reading file <br>"
Set ts = ("c:\", ForReading)
s =
"File contents = '" & s & "'"
End Sub
The following code demonstrates doing the same thing in JScript:
function ReadFiles()
{
var fso, f1, ts, s;
var ForReading = 1;
fso = new ActiveXObject("");
f1 = ("c:\\", true);
// Write a line.
("Writing file <br>");
("Hello World");
(1);
();
// Read the contents of the file.
("Reading file <br>");
ts = ("c:\\", ForReading);
s = ();
("File contents = '" + s + "'");
();
}
Move, copy and delete files
Each FSO object mode has two methods to move, copy and delete files, as described in the following table.
Task Method
Move file or
Copy the file or
Delete a file or
Please examine the sample code to understand two ways to delete files in FileSystemObject.
In the following VBScript example, create a text file in the root directory of drive C, write some information to it, then move it to the \tmp directory, and make a backup in \temp, and finally delete them from both directories.
To run the following example, you need to first create the \tmp and \temp directories in the root directory of drive C:
Sub ManipFiles
Dim fso, f1, f2, s
Set fso = CreateObject("")
Set f1 = ("c:\", True)
"Writing file <br>"
' Write a line.
("This is a test.")
' Close the file.
"Moving file to c:\tmp <br>"
' Get the handle of the file in the root directory of C (C:\).
Set f2 = ("c:\")
' Move the file to the \tmp directory.
("c:\tmp\")
"Copying file to c:\temp <br>"
' Copy the file to the \temp directory.
("c:\temp\")
"Deleting files <br>"
' Get the handle to the current location of the file.
Set f2 = ("c:\tmp\")
Set f3 = ("c:\temp\")
' Delete the file.
"All done!"
End Sub
The following code demonstrates doing the same thing in JScript:
function ManipFiles()
{
var fso, f1, f2, s;
fso = new ActiveXObject("");
f1 = ("c:\\", true);
("Writing file <br>");
// Write a line.
("This is a test.");
// Close the file.
();
("Moving file to c:\\tmp <br>");
// Get the handle of the file in the root directory of C (C:\).
f2 = ("c:\\");
// Move the file to the \tmp directory.
("c:\\tmp\\");
("Copying file to c:\\temp <br>");
// Copy the file to the \temp directory.
("c:\\temp\\");
("Deleting files <br>");
// Get the handle to the current location of the file.
f2 = ("c:\\tmp\\");
f3 = ("c:\\temp\\");
// Delete the file.
();
();
("All done!");
}