Método AtEndOfStream

Retornará True se o ponteiro do arquivo estiver no final de um arquivo TextStream; caso contrário, retornará False. Somente leitura.

Sintaxe

object.AtEndOfStream

Argumentos

Object: Obrigatório. Sempre o nome de um objeto TextStream.

Comentários

A propriedade AtEndOfStream só se aplica a arquivos TextStream abertos para leitura; caso contrário, ocorrerá um erro.

O código a seguir ilustra o uso da propriedade AtEndOfStream:

Exemplo 1:

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

' Usage
Dim fileContent
fileContent = ReadEntireFile("C:\Path\To\Your\File.txt")

Exemplo 2:

Function ReadUntilString(filespec, searchString)
    Const ForReading = 1
    Dim fso, theFile, line, foundString
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set theFile = fso.OpenTextFile(filespec, ForReading, False)
    foundString = False
    Do While theFile.AtEndOfStream <> True And foundString = False
        line = theFile.ReadLine
        If InStr(line, searchString) > 0 Then
            foundString = True
        End If
    Loop
    theFile.Close
    If foundString Then
        ReadUntilString = "String found: " & line
    Else
        ReadUntilString = "String not found."
    End If
End Function

' Usage
Dim result
result = ReadUntilString("C:\Path\To\Your\File.txt", "searchString")