TextStream Object:
Facilitates sequential access to file.
TextStream.{property | method( )}
The property and method arguments can be any of the properties and methods associated with the TextStream object. Note that in actual usage, TextStream is replaced by a variable placeholder representing the TextStream object returned from the FileSystemObject.
The following example illustrates the use of the TextStream object.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, ts, fileObj, TextLine, FileName
Set fso = CreateObject("Scripting.FileSystemObject")
' Create the file, and obtain a file object for the file.
FileName = "c:\testfile.txt"
fso.CreateTextFile FileName
Set fileObj = fso.GetFile(FileName)
' Open a text stream for output.
Set ts = fileObj.OpenAsTextStream(ForWriting, TristateUseDefault)
' Write to the text stream.
ts.WriteLine "Hello World!"
ts.WriteLine "The quick brown fox"
ts.Close
' Open a text stream for input.
Set ts = fileObj.OpenAsTextStream(ForReading, TristateUseDefault)
' Read from the text stream and display the results.
Do While ts.AtEndOfStream <> True
TextLine = ts.ReadLine
Document.Write TextLine & "
"
Loop
ts.Close
Close Method (FileSystemObject):
Closes an open TextStream file.
The object is always the name of a TextStream object.
The following example illustrates use of the Close method.
Sub CreateAFile
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
End Sub
CreateTextFile Method:
Creates a specified file name and returns a TextStream object that can be used to read from or write to the file.
object
Required. Always the name of a FileSystemObject or Folder object.
filename
Required. String expression that identifies the file to create.
overwrite
Optional. Boolean value that indicates whether you can overwrite an existing file. The value is true if the file can be overwritten, false if it can't be overwritten. If omitted, existing files are not overwritten.
unicode
Optional. 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, false if it's created as an ASCII file. If omitted, an ASCII file is assumed.
The following code illustrates how to use the CreateTextFile method to create and open a text file
Sub CreateAfile
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
End Sub
If the overwrite argument is false, or is not provided, for a filename that already exists, an error occurs.
Read Method:
Reads a specified number of characters from a TextStream file and returns the resulting string.
object.Read(characters)
object
Required. Always the name of a TextStream object.
characters
Required. Number of characters you want to read from the file.
The following example illustrates how to use the Read method to read a six character header from a file and return the resulting string:
Function ReadTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f, Msg
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
ReadTextFileTest = f.Read(5)
End Function
OpenTextFile Method:
Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file.
object.OpenTextFile(filename[, iomode[, create[, format]]])
object
Required. Object is always the name of a FileSystemObject.
filename
Required. String expression that identifies the file to open.
iomode
Optional. Can be one of three constants: ForReading, ForWriting, or ForAppending.
create
Optional. 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, False if it isn't created. If omitted, a new file isn't created.
format
Optional. One of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.
The iomode argument can have any of the following settings:
The format argument can have any of the following settings:
The following code illustrates the use of the OpenTextFile method.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
' The following line contains constants for the OpenTextFile
' format argument, which is not used in the code below.
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, MyFile, FileName, TextLine
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file for output.
FileName = "c:\testfile.txt"
Set MyFile = fso.OpenTextFile(FileName, ForWriting, True)
' Write to the file.
MyFile.WriteLine "Hello world!"
MyFile.WriteLine "The quick brown fox"
MyFile.Close
' Open the file for input.
Set MyFile = fso.OpenTextFile(FileName, ForReading)
' Read from the file and display the results.
Do While MyFile.AtEndOfStream <> True
TextLine = MyFile.ReadLine
Document.Write TextLine & "
"
Loop
MyFile.Close
OpenAsTextStream Method:
Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file.
object
.OpenAsTextStream([iomode, [format]])
object
Required. Always the name of a File object.
iomode
Optional. Indicates input/output mode. Can be one of three constants: ForReading, ForWriting, or ForAppending.
format
Optional. One of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.
The iomode argument can have any of the following settings:
The format argument can have any of the following settings:
The OpenAsTextStream method provides the same functionality as the OpenTextFile method of the FileSystemObject.
In addition, the OpenAsTextStream method can be used to write to a file.
The following code illustrates the use of the OpenAsTextStream method:
ReadAll Method:
Reads an entire TextStream file and returns the resulting string.
The object is always the name of a TextStream object.
For large files, using the ReadAll method wastes memory resources. Other techniques should be used to input a file, such as reading a file line by line.
The following example illustrates the use of the ReadAll method:
Function ReadAllTextFile
Const ForReading = 1, ForWriting = 2
Dim fso, MyFile, FileName
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file for output.
FileName = "c:\testfile.txt"
Set MyFile = fso.OpenTextFile(FileName, ForWriting, True)
' Write to the file.
MyFile.Write "Header"
MyFile.Write "1234567890987654321"
MyFile.Close
' Open the file for input.
Set MyFile = fso.OpenTextFile(FileName, ForReading)
' Read from the file.
If MyFile.AtEndOfStream Then
ReadAllTextFile = ""
Else
ReadAllTextFile = MyFile.ReadAll
End If
End Function
ReadLine Method:
Reads an entire line (up to, but not including, the newline character) from a TextStream file and returns the resulting string.
object.ReadLine( )
The object argument is always the name of a TextStream object.
The following example illustrates the use of the ReadLine method.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, MyFile, FileName, TextLine
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file for output.
FileName = "c:\testfile.txt"
Set MyFile = fso.OpenTextFile(FileName, ForWriting, True)
' Write to the file.
MyFile.WriteLine "Hello world!"
MyFile.WriteLine "The quick brown fox"
MyFile.Close
' Open the file for input.
Set MyFile = fso.OpenTextFile(FileName, ForReading)
' Read from the file and display the results.
Do While MyFile.AtEndOfStream <> True
TextLine = MyFile.ReadLine
Document.Write TextLine & "
"
Loop
MyFile.Close
Skip Method:
Skips a specified number of characters when reading a TextStream file.
object.Skip(characters):
object
Required. Always the name of a TextStream object.
characters
Required. Number of characters to skip when reading a file.
Skipped characters are discarded.
The following example illustrates the use of the Skip method:
Function SkipTextFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
f.Skip(6)
SkipTextFile = f.ReadLine
End Function
SkipLine Method:
Skips the next line when reading a TextStream file.
object.SkipLine( )
The object is always the name of a TextStream object.
The following examples illustrate the use of the SkipLine method:
Function SkipLineInFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!" & vbCrLf & "VBScript is fun!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
f.SkipLine
SkipLineInFile = f.ReadLine
End Function
Write Method (FileSystemObject):
Writes a specified string to a TextStream file.
object.Write(string)
object
Required. Always the name of a TextStream object.
string
Required. The text you want to write to the file.
Specified strings are written to the file with no intervening spaces or characters between each string. Use the WriteLine method to write a newline character or a string that ends with a newline character.
The following example illustrates the use of the Write method:
Function WriteToFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
WriteToFile = f.ReadLine
End Function
WriteBlankLines Method:
Writes a specified number of newline characters to a TextStream file.
object.WriteBlankLines(lines)
object
Required. Always the name of a TextStream object.
lines
Required. Number of newline characters you want to write to the file.
The following example illustrates the use of the WriteBlankLines method:
Function WriteBlankLinesToFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.WriteBlankLines 2
f.WriteLine "Hello World!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
WriteBlankLinesToFile = f.ReadAll
End Function
WriteLine Method (FileSystemObject):
Writes a specified string and newline character to a TextStream file.
object
Required. Always the name of a TextStream object.
string
Optional. The text you want to write to the file. If omitted, a newline character is written to the file.
Function WriteLineToFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.WriteLine "Hello world!"
f.WriteLine "VBScript is fun!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
WriteLineToFile = f.ReadAll
End Function
AtEndOfLine Property (FileSystemObject):
Returns true if the file pointer is positioned immediately before the end-of-line marker in a TextStream file; false if it is not. Read-only.
object.AtEndOfLine
The object is always the name of a TextStream object.
The AtEndOfLine property applies only to TextStream files that are open for reading; otherwise, an error occurs.
The following code illustrates the use of the AtEndOfLine property:
Function GetALine(filespec)
Dim fso, file, s
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile(filespec, ForReading, False)
s = ""
Do While file.AtEndOfLine <> True
s = s & file.Read(1)
Loop
file.Close
GetALine = s
End Function
AtEndOfStream Property (FileSystemObject):
Returns true if the file pointer is at the end of a TextStream file; false if it is not. Read-only.
object.AtEndOfStream
The object is always the name of a TextStream object.
The AtEndOfStream property applies only to TextStream files that are open for reading, otherwise, an error occurs.
The following code illustrates the use of the AtEndOfStream property:
Function ReadEntireFile(filespec)
Const ForReading = 1
Dim fso, theFile, retstring
Set fso = CreateObject("Scripting.FileSystemObject")
Set theFile = fso.OpenTextFile(filespec, ForReading, False)
Do While theFile.AtEndOfStream <> True
retstring = retstring & theFile.ReadLine
Loop
theFile.Close
ReadEntireFile = retstring
End Function
Column Property (FileSystemObject):
Read-only property that returns the column number of the current character position in a TextStream file.
The object is always the name of a TextStream object.
After a newline character has been written, but before any other character is written, Column is equal to 1.
The following examples illustrates the use of the Column property:
Function GetColumn
Const ForReading = 1, ForWriting = 2
Dim fso, f, m
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
f.Close
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
m = f.ReadLine
GetColumn = f.Column
End Function
Line Property (FileSystemObject):
Read-only property that returns the current line number in a TextStream file.
object.Line
The object is always the name of a TextStream object.
After a file is initially opened and before anything is written, Line is equal to 1.
The following example illustrates the use of the Line property:
Function GetLine
Const ForReading = 1, ForWriting = 2
Dim fso, f, ra
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!" & vbCrLf & "VBScript is fun!" & vbCrLf
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
ra = f.ReadAll
GetLine = f.Line
End Function
Facilitates sequential access to file.
TextStream.{property | method( )}
The property and method arguments can be any of the properties and methods associated with the TextStream object. Note that in actual usage, TextStream is replaced by a variable placeholder representing the TextStream object returned from the FileSystemObject.
The following example illustrates the use of the TextStream object.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, ts, fileObj, TextLine, FileName
Set fso = CreateObject("Scripting.FileSystemObject")
' Create the file, and obtain a file object for the file.
FileName = "c:\testfile.txt"
fso.CreateTextFile FileName
Set fileObj = fso.GetFile(FileName)
' Open a text stream for output.
Set ts = fileObj.OpenAsTextStream(ForWriting, TristateUseDefault)
' Write to the text stream.
ts.WriteLine "Hello World!"
ts.WriteLine "The quick brown fox"
ts.Close
' Open a text stream for input.
Set ts = fileObj.OpenAsTextStream(ForReading, TristateUseDefault)
' Read from the text stream and display the results.
Do While ts.AtEndOfStream <> True
TextLine = ts.ReadLine
Document.Write TextLine & "
"
Loop
ts.Close
Close Method (FileSystemObject):
Closes an open TextStream file.
The object is always the name of a TextStream object.
The following example illustrates use of the Close method.
Sub CreateAFile
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
End Sub
CreateTextFile Method:
Creates a specified file name and returns a TextStream object that can be used to read from or write to the file.
object
Required. Always the name of a FileSystemObject or Folder object.
filename
Required. String expression that identifies the file to create.
overwrite
Optional. Boolean value that indicates whether you can overwrite an existing file. The value is true if the file can be overwritten, false if it can't be overwritten. If omitted, existing files are not overwritten.
unicode
Optional. 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, false if it's created as an ASCII file. If omitted, an ASCII file is assumed.
The following code illustrates how to use the CreateTextFile method to create and open a text file
Sub CreateAfile
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
End Sub
If the overwrite argument is false, or is not provided, for a filename that already exists, an error occurs.
Read Method:
Reads a specified number of characters from a TextStream file and returns the resulting string.
object.Read(characters)
object
Required. Always the name of a TextStream object.
characters
Required. Number of characters you want to read from the file.
The following example illustrates how to use the Read method to read a six character header from a file and return the resulting string:
Function ReadTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f, Msg
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
ReadTextFileTest = f.Read(5)
End Function
OpenTextFile Method:
Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file.
object.OpenTextFile(filename[, iomode[, create[, format]]])
object
Required. Object is always the name of a FileSystemObject.
filename
Required. String expression that identifies the file to open.
iomode
Optional. Can be one of three constants: ForReading, ForWriting, or ForAppending.
create
Optional. 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, False if it isn't created. If omitted, a new file isn't created.
format
Optional. One of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.
The iomode argument can have any of the following settings:
Constant | Value | Description |
---|---|---|
ForReading | 1 | Open a file for reading only. You can't write to this file. |
ForWriting | 2 | Open a file for writing. |
ForAppending | 8 | Open a file and write to the end of the file. |
Constant | Value | Description |
---|---|---|
TristateUseDefault | -2 | Opens the file using the system default. |
TristateTrue | -1 | Opens the file as Unicode. |
TristateFalse | 0 | Opens the file as ASCII. |
The following code illustrates the use of the OpenTextFile method.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
' The following line contains constants for the OpenTextFile
' format argument, which is not used in the code below.
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, MyFile, FileName, TextLine
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file for output.
FileName = "c:\testfile.txt"
Set MyFile = fso.OpenTextFile(FileName, ForWriting, True)
' Write to the file.
MyFile.WriteLine "Hello world!"
MyFile.WriteLine "The quick brown fox"
MyFile.Close
' Open the file for input.
Set MyFile = fso.OpenTextFile(FileName, ForReading)
' Read from the file and display the results.
Do While MyFile.AtEndOfStream <> True
TextLine = MyFile.ReadLine
Document.Write TextLine & "
"
Loop
MyFile.Close
OpenAsTextStream Method:
Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file.
object
.OpenAsTextStream([iomode, [format]])
object
Required. Always the name of a File object.
iomode
Optional. Indicates input/output mode. Can be one of three constants: ForReading, ForWriting, or ForAppending.
format
Optional. One of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.
The iomode argument can have any of the following settings:
Constant | Value | Description |
---|---|---|
ForReading | 1 | Open a file for reading only. You can't write to this file. |
ForWriting | 2 | Open a file for writing. If a file with the same name exists, its previous contents are overwritten. |
ForAppending | 8 | Open a file and write to the end of the file. |
Constant | Value | Description |
---|---|---|
TristateUseDefault | -2 | Opens the file using the system default. |
TristateTrue | -1 | Opens the file as Unicode. |
TristateFalse | 0 | Opens the file as ASCII. |
The OpenAsTextStream method provides the same functionality as the OpenTextFile method of the FileSystemObject.
In addition, the OpenAsTextStream method can be used to write to a file.
The following code illustrates the use of the OpenAsTextStream method:
Const ForReading = 1, ForWriting = 2, ForAppending = 8 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Dim fso, ts, fileObj, TextLine Set fso = CreateObject("Scripting.FileSystemObject") ' Create the file, and obtain a file object for the file. FileName = "c:\testfile.txt" fso.CreateTextFile FileName Set fileObj = fso.GetFile(FileName) ' Open a text stream for output. Set ts = fileObj.OpenAsTextStream(ForWriting, TristateUseDefault) ' Write to the text stream. ts.WriteLine "Hello World!" ts.WriteLine "The quick brown fox" ts.Close ' Open a text stream for input. Set ts = fileObj.OpenAsTextStream(ForReading, TristateUseDefault) ' Read from the text stream and display the results. Do While ts.AtEndOfStream <> True TextLine = ts.ReadLine Document.Write TextLine & " " Loop ts.Close
ReadAll Method:
Reads an entire TextStream file and returns the resulting string.
The object is always the name of a TextStream object.
For large files, using the ReadAll method wastes memory resources. Other techniques should be used to input a file, such as reading a file line by line.
The following example illustrates the use of the ReadAll method:
Function ReadAllTextFile
Const ForReading = 1, ForWriting = 2
Dim fso, MyFile, FileName
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file for output.
FileName = "c:\testfile.txt"
Set MyFile = fso.OpenTextFile(FileName, ForWriting, True)
' Write to the file.
MyFile.Write "Header"
MyFile.Write "1234567890987654321"
MyFile.Close
' Open the file for input.
Set MyFile = fso.OpenTextFile(FileName, ForReading)
' Read from the file.
If MyFile.AtEndOfStream Then
ReadAllTextFile = ""
Else
ReadAllTextFile = MyFile.ReadAll
End If
End Function
ReadLine Method:
Reads an entire line (up to, but not including, the newline character) from a TextStream file and returns the resulting string.
object.ReadLine( )
The object argument is always the name of a TextStream object.
The following example illustrates the use of the ReadLine method.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, MyFile, FileName, TextLine
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file for output.
FileName = "c:\testfile.txt"
Set MyFile = fso.OpenTextFile(FileName, ForWriting, True)
' Write to the file.
MyFile.WriteLine "Hello world!"
MyFile.WriteLine "The quick brown fox"
MyFile.Close
' Open the file for input.
Set MyFile = fso.OpenTextFile(FileName, ForReading)
' Read from the file and display the results.
Do While MyFile.AtEndOfStream <> True
TextLine = MyFile.ReadLine
Document.Write TextLine & "
"
Loop
MyFile.Close
Skip Method:
Skips a specified number of characters when reading a TextStream file.
object.Skip(characters):
object
Required. Always the name of a TextStream object.
characters
Required. Number of characters to skip when reading a file.
Skipped characters are discarded.
The following example illustrates the use of the Skip method:
Function SkipTextFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
f.Skip(6)
SkipTextFile = f.ReadLine
End Function
SkipLine Method:
Skips the next line when reading a TextStream file.
object.SkipLine( )
The object is always the name of a TextStream object.
The following examples illustrate the use of the SkipLine method:
Function SkipLineInFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!" & vbCrLf & "VBScript is fun!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
f.SkipLine
SkipLineInFile = f.ReadLine
End Function
Write Method (FileSystemObject):
Writes a specified string to a TextStream file.
object.Write(string)
object
Required. Always the name of a TextStream object.
string
Required. The text you want to write to the file.
Specified strings are written to the file with no intervening spaces or characters between each string. Use the WriteLine method to write a newline character or a string that ends with a newline character.
The following example illustrates the use of the Write method:
Function WriteToFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
WriteToFile = f.ReadLine
End Function
WriteBlankLines Method:
Writes a specified number of newline characters to a TextStream file.
object.WriteBlankLines(lines)
object
Required. Always the name of a TextStream object.
lines
Required. Number of newline characters you want to write to the file.
The following example illustrates the use of the WriteBlankLines method:
Function WriteBlankLinesToFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.WriteBlankLines 2
f.WriteLine "Hello World!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
WriteBlankLinesToFile = f.ReadAll
End Function
WriteLine Method (FileSystemObject):
Writes a specified string and newline character to a TextStream file.
object
Required. Always the name of a TextStream object.
string
Optional. The text you want to write to the file. If omitted, a newline character is written to the file.
Function WriteLineToFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.WriteLine "Hello world!"
f.WriteLine "VBScript is fun!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
WriteLineToFile = f.ReadAll
End Function
AtEndOfLine Property (FileSystemObject):
Returns true if the file pointer is positioned immediately before the end-of-line marker in a TextStream file; false if it is not. Read-only.
object.AtEndOfLine
The object is always the name of a TextStream object.
The AtEndOfLine property applies only to TextStream files that are open for reading; otherwise, an error occurs.
The following code illustrates the use of the AtEndOfLine property:
Function GetALine(filespec)
Dim fso, file, s
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile(filespec, ForReading, False)
s = ""
Do While file.AtEndOfLine <> True
s = s & file.Read(1)
Loop
file.Close
GetALine = s
End Function
AtEndOfStream Property (FileSystemObject):
Returns true if the file pointer is at the end of a TextStream file; false if it is not. Read-only.
object.AtEndOfStream
The object is always the name of a TextStream object.
The AtEndOfStream property applies only to TextStream files that are open for reading, otherwise, an error occurs.
The following code illustrates the use of the AtEndOfStream property:
Function ReadEntireFile(filespec)
Const ForReading = 1
Dim fso, theFile, retstring
Set fso = CreateObject("Scripting.FileSystemObject")
Set theFile = fso.OpenTextFile(filespec, ForReading, False)
Do While theFile.AtEndOfStream <> True
retstring = retstring & theFile.ReadLine
Loop
theFile.Close
ReadEntireFile = retstring
End Function
Column Property (FileSystemObject):
Read-only property that returns the column number of the current character position in a TextStream file.
The object is always the name of a TextStream object.
After a newline character has been written, but before any other character is written, Column is equal to 1.
The following examples illustrates the use of the Column property:
Function GetColumn
Const ForReading = 1, ForWriting = 2
Dim fso, f, m
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
f.Close
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
m = f.ReadLine
GetColumn = f.Column
End Function
Line Property (FileSystemObject):
Read-only property that returns the current line number in a TextStream file.
object.Line
The object is always the name of a TextStream object.
After a file is initially opened and before anything is written, Line is equal to 1.
The following example illustrates the use of the Line property:
Function GetLine
Const ForReading = 1, ForWriting = 2
Dim fso, f, ra
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!" & vbCrLf & "VBScript is fun!" & vbCrLf
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
ra = f.ReadAll
GetLine = f.Line
End Function
No comments:
Post a Comment