9.15. TextStream Object
The TextStream
object provides powerful text file creation and manipulation
abilities. To create a new TextStream object,
invoke the FSO object's
CreateTextFile method:
Set objText = objFSO.CreateTextFile(strFileName, _The strFileName parameter identifies the
[bOverwrite],[bUnicode])
new filename. The optional bOverwrite
parameter will overwrite any existing files with same name if True.
The default value is True. The optional
bUnicode parameter will create a Unicode
file if True. The default is False.The following example creates a new text file:
Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")Once you have created a TextStream object, you are
Set objTextFile = objFSO.CreateTextFile("C:\Data\data.txt")
ready to write data to it. The Write or
WriteLine method will write data to the file:
objTextStream.Write(strText)The strText parameter is the text that
objTextStream.WriteLine(strText)
will be written to the file. The difference between the
Write and
WriteLine methods is that the
WriteLine method writes an end-of-line character
at the end of the line.Whenever you are done performing operations on a
TextStream object, invoke the
Close method. The
Close method closes the object and flushes any
updates to the file.
Dim objFSO, objTextFileTo open an existing text file, invoke the FSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile("D:\data.txt")
objTextFile.WriteLine "Write a line with end of line character"
objTextFile.Write "Write string without new line character"
objTextFile.Close
object's
OpenTextFile method:
Set objText = objFSO.OpenTextFile(strFileName, intIOMode, bCreate, intTrisState)Table 9-22 lists the
OpenTextFile method parameters.
Parameter | Description |
---|---|
strFileName | Filename to open. |
intIOMode | Optional. Specifies whether file is to be opened for Reading=1 or Appending=2. Default is reading. |
bCreate | Optional. If set to True, a new text file will be created if it is not found. Default is False. |
TriState | Optional. If -2, then open using filesystem settings; if -1, then use Unicode; and if 0, use ASCII. Default is 0, ASCII file. |
the whole file.There are three methods for reading data. The
ReadAll method returns the whole
text file as a string. ReadLine reads the line up
to the end-of-line character sequence. The Read
method reads a specified number of characters.
strData = objTextFile.ReadLine( ) 'read a single lineIf you are reading the file either character by character using the
strData = objTextFile.Read(10) 'read 10 characters
strData = objTextFile.ReadAll( ) 'read the whole file
Read method or line by line using the
ReadLine method, you need to be able to determine
when you hit the end of the file.The following example opens the data.txt file
and lists the contents of it:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("D:\data.txt")
Do While Not objTextFile.AtEndOfStream
strData = strData & objTextFile.ReadLine & vbCrLf
Loop
objTextFile.Close