Writing Text Files in WSH - Opening an existing text file
(Page 2 of 4 )
Now let’s take a look at opening an existing text file. The file system object provides the OpenTextFile method. The OpenTextFile method returns a TextStream object so don’t forget to use a Set statement when connecting. The OpenTextFile method uses a simple syntax.
object.OpenTextFile Filename [, Iomode[, Create[, Format]]]
Filename is a full path name to the file we would like to open. Imode takes a Constant value listed in Table 1. Its default is ForReading. Create is a Boolean value. True will create the text file if it doesn’t exist. Its default, False, will generate an error if the file does not exist. Finally, Format is a Tristate constant as listed in Table 2. It defaults to TristateFalse. ASCII is generally acceptable for all purposes. If you’re unsure, just leave it at the default.
Table 1: Constant Values for Iomode Attribute |
Constant | Value | Description |
ForReading | 1 | Read-Only |
ForWriting | 2 | Allow writing, overwrite contents |
ForAppending | 8 | Allow writing, append to file rather than overwrite |
| |
Table 2: Tristate Values for Format Attribute |
Constant | Value | Description |
TristateTrue | -1 | Unicode |
TristateFalse | 0 | ASCII |
TristateUseDefault | -2 | Use system default |
Be careful using the ForWriting constant as it does overwrite any existing information. If you wish to edit a file: read its contents first, make any changes, and then write it back in its entirety.
We’re going to begin our code by connecting to the FileSystemObject once again. Then we’re going to tell it to connect to our example text file and create it if it doesn’t exist. Let’s take a look at the code.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\ScriptLog.txt", 2, 1)
objFile.Write("Hello World")
objFile.Close
This simple snippet writes a single line to our text file and then closes the data stream. If you open the C:\ScriptLog.txt file in a text edit such as Notepad you should see the following result.
Hello World
We make use of the TextStream object’s Write method to write a string to our TextStream object. The syntax for the Write object is shown below. Now let’s run our example code again and write a few more strings to our file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\ScriptLog.txt", 2, 1)
objFile.Write("Hello World")
objFile.Write("This is a test")
objFile.Close
Now if we examine our file we should find that it contains the following text.
Hello WorldThis is a test
As you can see, the Write method simply writes our string to the file pointer’s current position in the file. We never told it to write to a new line so it just appended our second string to the first. There are two ways we can separate this to another line. We’ll look at both in our next example, but first let’s take a look at the syntax for our write methods.
object.Write(string)
object.WriteLine([string])
The write method has a single required parameter and will write the supplied string to a file at the current file pointer position. The WriteLine method will also write to the current file pointer position, but will add a newline character to the end of the supplied string. The string parameter for the WriteLine method is optional and if omitted it will simply write a single newline character.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\ScriptLog.txt", 2, 1)
objFile.Write("Hello World")
objFile.Write(vbCrLf)
objFile.WriteLine("This is a test")
objFile.Write("This text should be on a new line")
objFile.WriteLine
objFile.WriteLine("As should this")
objFile.Close
Our result should show each string on a new line. We’ve used several different methods in this code to make that happen.
Hello World
This is a test
This text should be on a new line
As should this
ß Note the blank line
The first two Write lines work together. The first wrote our string but left the file pointer on same line. The second Write line was used to manually write a line ending character. In the third line the WriteLine method was used to write a string complete with a linefeed.
Moving on, we used the Write method and the WriteLine method together. The Write method wrote “This text should be on a new line” to our file. Then we used the WriteLine method without any parameters to add a linefeed and move the file pointer to a new line.
Finally, in the last line we used the WriteLine method to write “As should this” to our file. Remember that the WriteLine method always ends with a newline character. That’s why our text file contained a blank line at the end.
Next: Advanced Writing Methods >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer