ZetCode

VBScript Open Method

last modified April 9, 2025

The Open method in VBScript is part of the FileSystemObject. It creates or opens a file and returns a TextStream object for reading or writing. This method is essential for file operations in VBScript. It provides various modes for different file handling needs.

Open allows specifying file access modes (read, write, append) and format (ASCII or Unicode). It handles file creation and access in a single operation. This tutorial covers Open with practical examples to demonstrate its usage.

Open Method Overview

The Open method takes three parameters: file path, I/O mode, and create flag. It returns a TextStream object for file operations. The method is available through the FileSystemObject in VBScript.

Key parameters include mode (ForReading, ForWriting, ForAppending) and format (TristateTrue, TristateFalse, TristateUseDefault). The method can create new files or open existing ones. Understanding these options enables flexible file handling in scripts.

Opening a File for Reading

This example demonstrates opening an existing file for reading. It shows the basic syntax for file access. The script reads the first line of the file and displays it. This is the simplest use of the Open method.

basic_read.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\data\example.txt", 1, False)
firstLine = file.ReadLine
WScript.Echo firstLine
file.Close

Set file = Nothing
Set fso = Nothing

The script creates a FileSystemObject and opens "example.txt" for reading (mode 1). It reads the first line using ReadLine and displays it. Always close files after use to free resources. The third parameter (False) specifies not to create the file if it doesn't exist.

Creating and Writing to a New File

This example shows how to create a new file and write content to it. The Open method creates the file if it doesn't exist. Writing mode overwrites existing files by default. This demonstrates basic file creation.

create_write.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\data\newfile.txt", 2, True)
file.WriteLine "This is the first line"
file.WriteLine "This is the second line"
file.Close

Set file = Nothing
Set fso = Nothing

The script opens "newfile.txt" in write mode (2). The third parameter (True) creates the file if it doesn't exist. Two lines are written using WriteLine. The file is automatically created in the specified location. Always close files after writing to ensure data is saved.

Appending to an Existing File

This example demonstrates appending content to an existing file. Append mode preserves existing content while adding new data. The file pointer starts at the end of the file. This is useful for log files and data collection.

append_file.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\data\log.txt", 8, True)
file.WriteLine "New log entry: " & Now()
file.Close

Set file = Nothing
Set fso = Nothing

The script opens "log.txt" in append mode (8). A new line with the current timestamp is added to the end. The third parameter (True) creates the file if missing. Append operations don't affect existing content. This pattern is common for logging operations.

Reading Entire File Contents

This example shows how to read an entire file at once. The ReadAll method loads all content into memory. This is useful for small files where memory isn't a concern. The script displays the complete file contents.

read_all.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\data\document.txt", 1, False)
content = file.ReadAll
WScript.Echo content
file.Close

Set file = Nothing
Set fso = Nothing

The script opens "document.txt" in read mode (1). ReadAll reads the entire content into a string variable. This approach is simple but uses memory proportional to file size. For large files, consider line-by-line reading. The third parameter (False) prevents file creation if missing.

Working with Unicode Files

This example demonstrates opening a Unicode-encoded file. The Open method supports different encoding formats. Unicode files require special handling for proper reading and writing. This shows how to specify the encoding type.

unicode_file.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\data\unicode.txt", 1, False, -1)
content = file.ReadAll
WScript.Echo content
file.Close

Set file = Nothing
Set fso = Nothing

The script opens "unicode.txt" with Unicode encoding (-1). The fourth parameter specifies TristateTrue for Unicode. This ensures proper handling of Unicode characters. The same parameter can specify ASCII (TristateFalse) or system default (TristateUseDefault). Always match the encoding to the file's actual format.

Source

FileSystemObject Documentation

In this article, we have explored the Open method in VBScript, covering its usage and practical applications. From basic reading to advanced Unicode handling, these examples demonstrate reliable file operations. With this knowledge, you can enhance your scripts with robust file management 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.