Section 9.15. TextStream Object

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   , _        [   bOverwrite   ],[   bUnicode   ]) 

The strFileName parameter identifies the 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") Set objTextFile = objFSO.CreateTextFile("C:\Data\data.txt") 

Once you have created a TextStream object, you are ready to write data to it. The Write or WriteLine method will write data to the file:

 objTextStream.Write(   strText   ) objTextStream.WriteLine(   strText   ) 

The strText parameter is the text that 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, objTextFile  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 

To open an existing text file, invoke the FSO object's OpenTextFile method:

 Set objText = objFSO.OpenTextFile(strFileName, intIOMode, bCreate, intTrisState) 

Table 9-22 lists the OpenTextFile method parameters.

Table 9-22. OpenTextFile 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.


If you have opened the file for read access, you can read in parts or 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 line strData = objTextFile.Read(10) 'read 10 characters strData = objTextFile.ReadAll( ) 'read the whole file 

If you are reading the file either character by character using the 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 



Windows XP in a Nutshell
Windows XP in a Nutshell, Second Edition
ISBN: 0596009003
EAN: 2147483647
Year: 2003
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net