Reading and writing files.
To read and write information to a file we can use two methods of the FileSystemObject object:
- CreateTextFile()
- OpenTextFile()
The method CreateTextFile() takes up to three arguments:
- the first argument [required] is the name of the file to create
- the second argument [optional] is a boolean variable that specifies either the method overwrites an existing file
or not. If this parameter is false (the default value), the existing file will not be overwritten.
If we need to create a new file with the name of already existing file we need to pass true as the second
argument. (???)
- the third argument [optional] is a boolean value that indicates whether the file is created as a Unicode or
ASCII file. The value is true if the file is created as a Unicode file, false if it's created
as an ASCII file. If omitted, an ASCII file is assumed.
If the method successfully opened the file it returns a TextStream object that can be used to read
from or write to the file. Once we created a file we can write to it using the same methods we used for
StdIn object. The following code creates
a file test.txt and writes a short sentence to it
var fs = new ActiveXObject("Scripting.FileSystemObject");
var file = fs.CreateTextFile("test.txt");
file.WriteLine("I'm just trying to write something to the file");
file.Close();
The method OpenTextFile() Opens a specified file and returns a TextStream object that can be
used to read from, write to, or append to the file. The general syntax of the method is
fs_object.OpenTextFile( filename [, iomode[, create[, format]]])
where
- filename is the name of the file to open [required]
- iomode is the mode we are opening the file in. Can be one of the values from the table. [optional]
| Value | Description |
| 1 | Open a file for reading only. You can't write to this file. |
| 2 | Open a file for writing |
| 8 | Open a file and write to the end of the file. |
- create is a Boolean value that indicates whether a new file can be created if the specified filename doesn't
exist. The value is true if a new file is created, false if it isn't created. If omitted, a new
file isn't created. [optional]
- format is one of three Tristate values used to indicate the format of the opened file. If omitted,
the file is opened as ASCII.
| Value | Description |
| TristateTrue | Open the file as Unicode. |
| TristateFalse | Open the file as ASCII. |
| TristateUseDefault | Open the file using the system default. |
In the following example we will open the file test.txt for appending and add a couple of lines in the file.
If the file doesn't exist it will be created.
var ForAppending = 8;
var fs = new ActiveXObject("Scripting.FileSystemObject");
var file = fs.OpenTextFile("test.txt", ForAppending, true);
file.WriteLine("one more line");
file.WriteLine("last line");
file.Close();
To open a file use code like this
var ForReading = 1;
var fs = new ActiveXObject("Scripting.FileSystemObject");
var file = fs.OpenTextFile("test.txt", ForReading);
var msg = "";
msg += file.ReadLine() + "\n";
while( ! file.AtEndOfStream ){
msg += file.ReadLine() + "\n";
}
file.Close();
WScript.Echo(msg);
|
Please note that we deal with open files (read or write) exactly the same way we dealt with the standard input/output
streams, because they all are objects of the TextStream type.
|
References