ZetCode

VBScript Read Method

last modified April 9, 2025

The Read method in VBScript is part of the TextStream object from the FileSystemObject. It reads a specified number of characters from a text file. This method is essential for file input operations in VBScript. It allows precise control over how much data to read at once.

Read is commonly used with other TextStream methods like ReadLine and ReadAll. It's particularly useful for processing large files in chunks. This tutorial covers Read with practical examples to demonstrate its usage.

Read Method Overview

The Read method takes one parameter: the number of characters to read. It returns a string containing the specified number of characters. The method advances the file pointer by the number of characters read.

Key features include character-by-character reading and position tracking. It reads from the current position in the file. Read works with files opened in read mode. Understanding this method helps create efficient file processing scripts.

Basic File Reading

This example demonstrates the simplest use of Read to read a specific number of characters from a file. It shows how to open a file and read its contents in chunks. The script reads the first 10 characters from a text file.

basic_read.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\data\sample.txt", 1) ' 1 = ForReading

firstChars = file.Read(10)
WScript.Echo "First 10 characters: " & firstChars

file.Close
Set file = Nothing
Set fso = Nothing

The script creates a FileSystemObject and opens a file for reading. Read(10) reads the first 10 characters from the file. The result is displayed using WScript.Echo. Always close files after reading.

Reading File in Chunks

This example shows how to read a file in fixed-size chunks using a loop. It demonstrates processing large files without loading everything into memory. The script reads 100 characters at a time until EOF.

read_chunks.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\data\largefile.txt", 1)

Do Until file.AtEndOfStream
    chunk = file.Read(100)
    WScript.Echo "Read chunk: " & chunk
Loop

file.Close
Set file = Nothing
Set fso = Nothing

The script opens a file and enters a loop that continues until EOF. Each iteration reads 100 characters using Read(100). This approach is memory-efficient for large files. The loop terminates when AtEndOfStream becomes true.

Combining Read with Other Methods

This example demonstrates combining Read with ReadLine for mixed reading operations. It shows how to read specific characters and then switch to line reading. The script reads the first 20 chars then the next line.

mixed_reading.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\data\logfile.txt", 1)

header = file.Read(20)
WScript.Echo "Header: " & header

firstLine = file.ReadLine()
WScript.Echo "First line after header: " & firstLine

file.Close
Set file = Nothing
Set fso = Nothing

The script first reads 20 characters as a header section. Then it reads the remaining part of the line with ReadLine. This technique is useful for structured files with headers. The file pointer moves appropriately between operations.

Reading Specific Sections

This example shows how to read specific sections of a file by skipping characters. It demonstrates using Skip with Read for targeted reading. The script skips 50 chars then reads the next 30.

targeted_read.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\data\config.txt", 1)

file.Skip(50) ' Skip first 50 characters
section = file.Read(30)
WScript.Echo "Section from 50-80: " & section

file.Close
Set file = Nothing
Set fso = Nothing

The script opens a file and skips the first 50 characters. Then it reads the next 30 characters into a variable. This approach is useful for fixed-format files. Skip moves the file pointer without reading data.

Error Handling with Read

This example demonstrates proper error handling when using the Read method. It shows how to manage potential file reading errors gracefully. The script includes checks for file existence and read operations.

error_handling.vbs
On Error Resume Next

Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists("C:\data\important.txt") Then
    WScript.Echo "Error: File not found"
    WScript.Quit 1
End If

Set file = fso.OpenTextFile("C:\data\important.txt", 1)
If Err.Number <> 0 Then
    WScript.Echo "Error opening file: " & Err.Description
    WScript.Quit 1
End If

content = file.Read(100)
If Err.Number <> 0 Then
    WScript.Echo "Error reading file: " & Err.Description
Else
    WScript.Echo "Read content: " & content
End If

file.Close
Set file = Nothing
Set fso = Nothing

The script includes checks for file existence and proper error handling. It uses On Error Resume Next to manage runtime errors. Each operation is followed by error checking. This makes the script more robust for production use.

Source

TextStream Object Documentation

In this article, we have explored the Read method in VBScript, covering its usage and practical applications. From basic reading to advanced error handling, these examples demonstrate reliable file processing. With this knowledge, you can enhance your file handling scripts with precise reading capabilities.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all VBScript tutorials.