VBScript ReadLine Method
last modified April 9, 2025
The ReadLine method in VBScript is part of the
FileSystemObject TextStream object. It reads one line from a text
file and returns it as a string. This method is essential for processing text
files line by line. It automatically advances to the next line after reading.
ReadLine is commonly used in file processing scripts to handle log
files, configuration files, and data files. It stops at each line break
character sequence. This tutorial covers ReadLine with practical
examples to demonstrate its usage.
ReadLine Method Overview
The ReadLine method reads a single line from an open text file. It
returns the line content without the line break characters. The method requires
a TextStream object opened for reading. It moves the file pointer to the next
line after each call.
Key features include automatic line break handling and sequential reading. It returns an empty string for blank lines. At end-of-file, it raises an error. Understanding this method helps create robust file processing scripts.
Basic File Reading
This example demonstrates the simplest use of ReadLine to read a
file line by line. It shows how to open a file, read its contents, and close it.
The script outputs each line to the console.
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\temp\sample.txt", 1) ' 1 = ForReading
Do Until file.AtEndOfStream
line = file.ReadLine
WScript.Echo line
Loop
file.Close
Set fso = Nothing
The script creates a FileSystemObject and opens a text file. The
Do Until loop reads each line until end-of-file. Each line is
displayed using WScript.Echo. Finally, resources are cleaned up.
Processing CSV File
This example shows how to process a CSV file using ReadLine. It
reads each line and splits it into fields. The script demonstrates basic data
parsing from a structured text file.
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\data\employees.csv", 1)
' Skip header line if needed
file.ReadLine
Do Until file.AtEndOfStream
line = file.ReadLine
fields = Split(line, ",")
WScript.Echo "Name: " & fields(0) & ", Department: " & fields(2)
Loop
file.Close
Set fso = Nothing
The script reads a CSV file containing employee data. Each line is split into fields using the comma delimiter. The script then outputs specific fields. Note how the header line can be skipped if needed.
Counting Lines in a File
This example demonstrates using ReadLine to count lines in a file.
It shows how to process a file while maintaining a counter. The script provides
basic file statistics.
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\logs\app.log", 1)
lineCount = 0
Do Until file.AtEndOfStream
file.ReadLine
lineCount = lineCount + 1
Loop
WScript.Echo "Total lines: " & lineCount
file.Close
Set fso = Nothing
The script opens a log file and initializes a counter. Each ReadLine
call increments the counter. The total line count is displayed at the end. This
pattern is useful for file analysis tasks.
Searching File Contents
This example shows how to search for specific text in a file using
ReadLine. It reads each line and checks for a match. The script
demonstrates basic text search functionality.
searchTerm = "ERROR"
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\logs\system.log", 1)
lineNumber = 0
Do Until file.AtEndOfStream
line = file.ReadLine
lineNumber = lineNumber + 1
If InStr(line, searchTerm) > 0 Then
WScript.Echo "Found at line " & lineNumber & ": " & line
End If
Loop
file.Close
Set fso = Nothing
The script searches for "ERROR" in a log file. Each line is checked using
InStr. Matching lines are displayed with their line numbers. This
approach is useful for log analysis and debugging.
Reading Configuration File
This example demonstrates reading a configuration file with
ReadLine. It processes key-value pairs separated by equals signs.
The script shows how to parse simple configuration files.
Set config = CreateObject("Scripting.Dictionary")
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\app\config.ini", 1)
Do Until file.AtEndOfStream
line = file.ReadLine
If InStr(line, "=") > 0 Then
parts = Split(line, "=")
key = Trim(parts(0))
value = Trim(parts(1))
config.Add key, value
End If
Loop
' Access configuration values
WScript.Echo "Timeout: " & config("timeout")
file.Close
Set fso = Nothing
The script reads a configuration file storing settings in a Dictionary. Each key-value pair is split and trimmed. The configuration can then be accessed through the Dictionary object. This pattern is useful for script configuration.
Source
FileSystemObject Documentation
In this article, we have explored the ReadLine method in VBScript,
covering its usage and practical applications. From simple file reading to
complex data processing, these examples demonstrate line-based file handling.
With this knowledge, you can enhance your file processing scripts.
Author
List all VBScript tutorials.