ZetCode

VBScript Save Method

last modified April 9, 2025

The Save method in VBScript is used to persist data to files. It's commonly available in objects like TextStream and XML DOM objects. This method writes the current content to a specified file location. It's essential for data persistence in scripting scenarios.

Save operations can overwrite existing files or create new ones. Understanding this method is crucial for file manipulation tasks. This tutorial covers Save with practical examples demonstrating various use cases.

Save Method Overview

The Save method typically takes a file path as its parameter. It writes the object's current content to the specified location. The exact behavior depends on the object type being saved. Some objects may offer additional save options.

Key features include file creation, content overwriting, and encoding handling. The method doesn't typically return a value but may raise errors. Understanding file system permissions is important when using Save.

Saving Text to a File

This example demonstrates saving text content to a file using TextStream. It shows basic file creation and text writing. The script creates a new file and writes sample content to it.

basic_save.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile("C:\temp\example.txt", True)
file.WriteLine "This is sample text content."
file.WriteLine "Second line of the file."
file.Close

WScript.Echo "File saved successfully"

The script creates a FileSystemObject and makes a new text file. The CreateTextFile method prepares the file for writing. WriteLine adds content, and Close saves it. The True parameter forces overwrite if the file exists.

Saving XML Document

This example shows saving an XML DOM document to a file. It demonstrates XML processing and file saving in VBScript. The script creates a simple XML structure then saves it.

xml_save.vbs
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
Set root = xmlDoc.createElement("root")
xmlDoc.appendChild root

Set item = xmlDoc.createElement("item")
item.Text = "Sample content"
root.appendChild item

xmlDoc.Save "C:\temp\data.xml"
WScript.Echo "XML document saved"

The script creates an XML document object and builds a simple structure. The Save method writes the XML to disk. This approach is useful for configuration files or data exchange. The XML DOM provides various save options.

Appending to Existing File

This example demonstrates appending content to an existing file. It shows how to open a file in append mode rather than overwriting. The script adds new lines to the file while preserving existing content.

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

WScript.Echo "Log entry appended"

The script opens the file in append mode (8) and creates it if needed (True). Each run adds a new timestamped line to the file. This is useful for log files or ongoing data collection. The method preserves all previous content.

Saving with Different Encoding

This example shows saving text files with specific character encoding. VBScript can save files in various encodings like Unicode or ASCII. The script demonstrates creating a Unicode text file.

encoding_save.vbs
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile("C:\temp\unicode.txt", True, True)
file.WriteLine "This text will be saved in Unicode format."
file.Close

WScript.Echo "Unicode file saved"

The third parameter in CreateTextFile specifies Unicode (True) or ASCII (False). This example creates a Unicode-encoded text file. Different encodings are useful for international character support. The method handles the conversion automatically.

Saving Dictionary to File

This advanced example shows saving a Dictionary object's contents to file. The script serializes the dictionary to a text format before saving. It demonstrates data structure persistence.

dictionary_save.vbs
Set dict = CreateObject("Scripting.Dictionary")
dict.Add "Name", "John Doe"
dict.Add "Age", "35"
dict.Add "Occupation", "Developer"

Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile("C:\temp\userdata.txt", True)

For Each key In dict.Keys
    file.WriteLine key & "=" & dict(key)
Next

file.Close
WScript.Echo "Dictionary saved to file"

The script creates a Dictionary and populates it with sample data. It then iterates through the items, writing key-value pairs to the file. This pattern can be extended for complex data structures. The resulting file is human- readable and parseable.

Source

FileSystemObject Documentation

In this article, we have explored the Save method in VBScript, covering its various applications. From simple text files to complex data structures, these examples demonstrate reliable file operations. With this knowledge, you can implement robust data persistence in your scripts.

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.