Creating a Text File Using WSH

Windows Scripting Host (WSH) offers three methods for creating text files: CreateTextFile, OpenTextFile, and OpenAsTextStream. The syntax of each of these methods and example procedures are presented in the following table.

Table 8-7: Various methods of creating text files Method/Syntax Example

CreateTextFile object.CreateTextFile(filename[, overwrite[, unicode]])

Object is the name of the FileSystemObject or the Folder object.

Filename is a string expression that specifies the file to create.

Overwrite (optional) is a Boolean value that indicates whether you can overwrite an existing file. The value is True if the file can be overwritten and False if it can't be overwritten. If omitted, existing files are not overwritten.

Unicode (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 and False if it's created as an ASCII file. If omitted, an ASCII file is assumed.

Sub CreateFile_Method1() Dim fs, objFile

Set fs = CreateObject("Scnpting.FileSystemObject") Set objFile = fs.CreateTextFile("C:\Phones.txt", True) objFile.WriteLine ("Margaret Kubiak: 212-338-8778") objFile.WriteBlankLines (2)

objFile.WriteLine ("Robert Prochot: 202-988-2331") objFile.Close End Sub

The above procedure creates a text file to store the names and phone numbers of two people. Because there is a Boolean value of True in the position of the overwrite argument, the C:\Phones.txt file will be overwritten if it already exists in the specified folder.

OpenTextFile object.OpenTextFile(filename[, iomode[, create[, format]]])

Object is the name of the FileSystemObject.

Filename is a string expression that identifies the file to open.

Iomode (optional) 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 and False if it isn't created. If omitted, a new file isn't created. The iomode argument can be one of the following constants:

Method/Syntax Example

OpenTextFile ForReading (1)

ForAppending (8)

Create (optional) 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 and False if it isn't created. If omitted, a new file isn't created.

Format (optional) is one of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.

TristateTrue = Open the file as ASCII. TristateFalse = Open the file as Unicode. TristateUseDefault = Open the file using the system default.

Sub CreateFile_Method2() Dim fs, objFile

Set fs = CreateObject("Scripting.FileSystemObject") Set objFile = fs.OpenTextFile("C:\Shopping.txt", _

ForWriting, True)

objFile.WriteLine ("Bread") objFile.WriteLine ("Milk") objFile.WriteLine ("Strawberries") objFile.Close End Sub

OpenAsTextStream object.OpenAsTextStream([iomode, [format]]) Object is the name of the File object.

Iomode (optional) indicates input/output mode. It can be one of three constants:

ForReading (1)

ForWriting (2)

ForAppending (8)

Format (optional) is one of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.

TristateTrue = Open the file as ASCII. TristateFalse = Open the file as Unicode. TristateUseDefault = Open the file using the system default.

Sub CreateFile_Method3() Dim fs, objFile, objText

Set fs = CreateObject("Scripting.FileSystemObject")

fs.CreateTextFile "New.txt"

Set objFile = fs.GetFile("New.txt")

Set objText = objFile.OpenAsTextStream(ForWriting, _

TristateUseDefault) objText.Write "Wedding Invitation" objText.Close

Set objText = objFile.OpenAsTextStream(ForReading, _ TristateUseDefault)

Method/Syntax

Example

OpenAsTextStream (cont.)

MsgBox objText.ReadLine objText.Close End Sub

0 0

Post a comment