58
VBScript Reference Manual
InduSoft Web Studio
250
InduSoft, Ltd.
TextStream Object
The TextStream Object allows the programmer to sequentially access a text file. Once the TextStream
object is instantiated, it can be referred to as an object from VBScript and its various Methods and
Properties accessed.
The TextStream object can be instantiated in three different ways. These are
• Through the CreateTextFile method of the FSO object
• Through the OpenTextFile method of the FSO object
• Through the OpenAsTextStream method of the File Object
There are subtle differences between these methods. The CreateTextFile is used to create a file and a
TextStream object. This method can optionally overwrite an existing object. The OpenTextFile opens
an existing file and returns a TextStream object, but can optionally create the filename if it does not
exist. The OpenAsTextStream object opens an existing file and returns a TextStream object. This
method gives an error if the text file does not exist, there is no option to create the file if it does not exist.
Another difference is that the CreateTextFile method opens a TextStream object for reading and
writing, while the OpenTextFile and OpenAsTextStream methods open a TextStream object for
or
reading, writing or appending.
Examples of the various approaches to instantiating the TextStream object are:
Instantiating a TextStream object with the CreateTextFile Method
Dim fso, f, myfile
Set fso = CreateObject(“Scripting.FileSystemObject”)
‘Instantiate the FSO object
myFile = $getAppPath() & “notes.txt”
‘Specify the app directory & file
Set f = fso.CreateTextFile(myFile, True, True)
‘Open as UniCode TextStream
object
Instantiating a TextStream object with the OpenTextFile Method
Constant forReading = 1, forWriting = 2, forAppending = 8
Dim fso, myfile, tso
Set fso = CreateObject(“Scripting.FileSystemObject”)
‘Instantiate the FSO object
myFile = $getAppPath() & “notes.txt”
‘Specify the app directory & file
Set tso = fso.OpenTextFile(myFile, ForWriting, True, True) ‘Open as UniCode TextStream
object
Instantiating a TextStream object with the OpenAsTextStream Method
Constant forReading = 1, forWriting = 2, forAppending = 8
Dim fso, f, myfile, tso
Set fso = CreateObject(“Scripting.FileSystemObject”)
‘Instantiate the FSO object
myFile = $getAppPath() & “notes.txt”
‘Specify the app directory & file
Set f = fso.GetFile(myFile)
‘Instantiate the file object
Set tso = f.OpenAsTextStream(forAppending, True)
‘Open as UniCode TextStream
object
See the CreateTextFile and OpenTextFile methods under the FileSystemObject Object Model section
n
for additional details on instantiation of the TextStream Object. See the OpenAsTextStream method
under the File Object section for additional details on instantiation of the TextStream Object
The TextStream object supports either ASCII or UniCode characters, according to the argument
settings when calling the method used to instantiate the TextStream object.