VBScript Open Method
last modified April 9, 2025
The Open
method in VBScript is part of the
FileSystemObject
. It creates or opens a file for reading or writing.
This method provides access to file contents with various modes and permissions.
It's essential for file manipulation in VBScript scripts.
Open
allows specifying access modes like read, write, or append. It
can create new files or open existing ones. This tutorial covers Open
with practical examples to demonstrate its usage in different scenarios.
Open Method Overview
The Open
method takes several parameters: file path, I/O mode,
create flag, and format. It returns a TextStream object for file operations.
The method is available through the FileSystemObject
in VBScript.
Key features include different access modes and file creation options. It handles
both ASCII and Unicode file formats. Open
is fundamental for reading
and writing files in scripts. Understanding this method enables robust file
handling in VBScript.
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.
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". The
second parameter (1) specifies read-only mode. ReadLine
gets the
first line, which is then displayed. Always close files after use.
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. The script writes
two lines of text to the new file. This demonstrates write mode usage.
Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.OpenTextFile("C:\data\newfile.txt", 2, True) file.WriteLine "This is line one" file.WriteLine "This is line two" file.Close Set file = Nothing Set fso = Nothing
The second parameter (2) specifies write mode. The third parameter (True) allows
file creation. WriteLine
adds text with line breaks. The file is
created in the specified location with the given content.
Appending to an Existing File
This example demonstrates appending content to an existing file. The script opens the file in append mode and adds new lines. Existing content remains unchanged. This is useful for log files or data collection.
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 second parameter (8) specifies append mode. The script adds a timestamped entry to the log file. If the file doesn't exist, it will be created. Append mode preserves existing content while adding new data.
Reading Entire File Contents
This example shows how to read an entire file at once. The script uses the
ReadAll
method after opening the file. This approach is useful for
small files where memory isn't a concern. The content is displayed as a single
string.
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 the file in read mode (1). ReadAll
reads all
content into memory at once. This is efficient for small files but avoid it for
large files. The entire content is then displayed.
Working with Unicode Files
This example demonstrates opening and reading a Unicode file. The script specifies the Unicode format when opening the file. This ensures proper handling of non-ASCII characters. The example reads and displays the content.
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 fourth parameter (-1) specifies Unicode format. Without this, Unicode characters may display incorrectly. This is essential for international text files. The script reads and displays the Unicode content properly.
Source
FileSystemObject Documentation
In this article, we have explored the Open
method in VBScript,
covering its usage and practical applications. From reading files to writing and
appending, these examples demonstrate essential file operations. With this
knowledge, you can implement robust file handling in your VBScript projects.
Author
List all VBScript tutorials.