ReadAll Method

Updated: September 2009

Reads an entire TextStream file and returns the resulting string.


                      object.ReadAll( );

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()
{
    var ForReading = 1, ForWriting = 2;
    var fso = new ActiveXObject("Scripting.FileSystemObject");

    // Open the file for output.
    var filename = "c:\\testfile.txt";

    var f = fso.OpenTextFile(filename, ForWriting, true);

    // Write to the file.
    f.Write("Header");
    f.Write("1234567890987654321");
    f.Close();

    // Open the file for input.
    f = fso.OpenTextFile(filename, ForReading);

    // Read from the file.
    if (f.AtEndOfStream)
        return ("");
    else
        return (f.ReadAll());
}
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

Applies To:

Date

History

Reason

September 2009

Modified the examples.

Customer feedback.

Community Additions

Show: