Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

TextStream is VBScript’s sequential text-file interface. A FileSystemObject creates or opens the stream; the TextStream object then reads, writes, moves through, and closes the file. Use ReadLine for line-by-line work, Read for a specific character count, and ReadAll when loading the complete file is appropriate.

What the TextStream object does

Microsoft defines TextStream as an object that “facilitates sequential access to file.” Sequential means the stream has a current position, or pointer, and operations consume or add content at that position. It is designed for text processing rather than random-access byte operations.

You normally obtain a TextStream from a Scripting.FileSystemObject call such as CreateTextFile or OpenTextFile. The FileSystemObject handles creation or opening; TextStream supplies the actual read, write, position, and close operations.

Creating and writing a text file

Microsoft’s documented write pattern creates the FileSystemObject, obtains a writable TextStream, writes a line, and closes it:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:testfile.txt", True)
a.WriteLine("This is a test.")
a.Close

The True argument allows an existing file to be overwritten. WriteLine writes the supplied text and adds a newline. Calling Close ends the stream when the work is complete.

Write, WriteLine, and WriteBlankLines

Method Effect Use it when
Write(text) Writes text without adding a line ending. You are assembling a line or controlling separators yourself.
WriteLine(text) Writes text followed by a newline. Each call represents one complete output line.
WriteBlankLines(n) Writes the requested number of newline characters. You need blank lines or deliberate vertical spacing.

Reading a file line by line

For log files, configuration records, and other line-oriented input, open a TextStream and continue until AtEndOfStream becomes true:

Set fs = CreateObject("Scripting.FileSystemObject")
Set input = fs.OpenTextFile("c:input.txt")

Do Until input.AtEndOfStream
    line = input.ReadLine
    WScript.Echo line
Loop

input.Close

ReadLine advances the current position by one line. Testing AtEndOfStream in the loop condition prevents a read after the end of the file. Close the stream after the loop, including in longer scripts where error handling may also be needed.

Choosing Read, ReadLine, or ReadAll

Method What it returns Best fit Trade-off
Read(n) A specified number of characters. Character-count processing or fixed-size chunks. You must decide how many characters to request and manage the pointer yourself.
ReadLine The next line. Records, logs, and other line-oriented formats. It follows line boundaries rather than a requested character count.
ReadAll The entire remaining file content. Small files that can be processed as one string. Whole-file loading uses more memory than incremental reading.

The practical decision is incremental versus whole-file processing: use ReadLine or Read when you want to process input as it arrives, and reserve ReadAll for files whose complete contents can be loaded comfortably.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Moving through the stream

Skip characters

Skip(n) advances the pointer past a specified number of characters without returning them. This is useful when a known prefix or fixed-width portion is not needed.

Skip a line

SkipLine advances past the next line without returning its text. It is useful for discarding headers or records that do not need processing.

These operations are still sequential: they move forward from the current pointer rather than jumping to an arbitrary byte offset.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Checking position with TextStream properties

Property Meaning
AtEndOfStream True when the pointer is at the end of the file. Use it to control read loops.
AtEndOfLine True when the pointer is immediately before the end-of-line marker.
Column The current character column.
Line The current line number.

For ordinary line-by-line reading, AtEndOfStream is the key guard. Line and Column can help report or inspect the current position while processing input.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Closing a TextStream correctly

Call Close after reading or writing:

stream.Close

Closing releases the open file handle and marks the end of the stream session. A typical script closes immediately after its final operation, as in the Microsoft creation example. Do not leave a stream open while continuing unrelated work.

Common patterns at a glance

  • Create and write: CreateTextFile, then Write or WriteLine, then Close.
  • Read each record: OpenTextFile, loop while Not AtEndOfStream, call ReadLine, then Close.
  • Read a fixed amount: call Read(n) and account for the pointer moving forward.
  • Read everything: call ReadAll when whole-file memory use is acceptable.
  • Discard input: use Skip(n) for characters or SkipLine for a complete line.

What TextStream does not provide

TextStream exposes sequential text operations, not a random-access byte API. The reference material also does not establish encoding rules or Unicode edge-case behavior; scripts that depend on a particular encoding should verify that behavior against the relevant VBScript and Windows documentation instead of assuming that TextStream performs a specific conversion.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.