Read Write Text Files Using VBScript – Master File Operations

Working with text files is a common requirement in test automation. In this guide, we’ll walk you through how to read write text files using VBScript. If you’re new to VBScript or looking to expand your automation toolkit, this tutorial will give you a solid foundation for managing text files using VBScript.

First the basics. VBscript has objects to handle various types of tasks. In order to handle and work with text files, you need to leverage FileSystemObject (FSO) object. This object has inbuilt functions for following tasks.

  • Open a Text file
  • Create a Textfile
  • Delete a Text file
  • Write a text file
  • Append data into a text file
  • Rename a text file
  • And many more…

For files operation you should remember 3 numbers in mind. 1,2, and 8. These will be used over and over again. These are modes of operation in a text file.

  • 1 – Read -> This will open a file to read
  • 2 – Write a file – > This will open a file to write
  • 8 – Append -> This will open a file in Append mode, which means if you have an existing data, the input data will be appended in the existing data.

I want to thank Devguru for the absolutely amazing content they have put together for VBScript. My first guide which helped me gain extensive knowledge in VBScript. You should refer it too.

How to Read a Text File Using VBScript

Let’s see how we can read a text file using VBScript. Before we go into the code, make sure you have access to a text file you want to read. In this example, we’ll use the OpenTextFile method from the FileSystemObject (FSO) in VBScript.

Code to Read a Text File
' Create the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Open the text file in read mode (1 = ForReading)
Set objFile = objFSO.OpenTextFile("C:\path\to\yourfile.txt", 1)
' Loop through each line in the file and display it
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    WScript.Echo strLine
Loop
' Close the file after reading
objFile.Close
'read write text files using vbscript
Explanation of the Code
  • FileSystemObject: This object handles all file operations in VBScript.
  • OpenTextFile Method: This method opens the specified file. The second argument, 1, signifies “ForReading” mode.
  • AtEndOfStream Property: This property checks if you’ve reached the end of the file.
  • ReadLine Method: This method reads each line of text until the end of the file. Instead of ReadLine if you use ReadAll, It will read all content at once.

For more information about the FileSystemObject, refer to Microsoft’s documentation on VBScript FileSystemObject.

How to Write to a Text File Using VBScript

Now, let’s look at how to write data to a text file. Writing to files is essential when generating logs or creating configuration files. Here’s a simple example to get you started.

Code to Write to a Text File
' Create the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Create or open the text file in write mode (2 = ForWriting)
Set objFile = objFSO.OpenTextFile("C:\path\to\yourfile.txt", 2, True)
' Write some content to the file
objFile.WriteLine "This is a sample line written to the file."
objFile.WriteLine 
' Close the file after writing
objFile.Close
'read write text files using vbscript
Explanation of the Code
  • ForWriting: The 2 in the OpenTextFile method sets the mode to “ForWriting”.
  • True: The third argument creates the file if it doesn’t exist.
  • WriteLine Method: This method writes a line to the text file, automatically appending a new line.

To learn more about handling file operations with VBScript, check out Microsoft’s VBScript Guide.

Advanced Techniques: Appending to a Text File

Sometimes, you may want to add data to an existing file without overwriting it. In VBScript, you can use the ForAppending mode for this purpose. The number is 8.

Code to Append to a Text File
' Create the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Open the text file in append mode (8 = ForAppending)
Set objFile = objFSO.OpenTextFile("C:\path\to\yourfile.txt", 8, True)
' Append content to the file
objFile.WriteLine "Appending a new line to the file."
' Close the file after appending
objFile.Close
'read write text files using vbscript
Explanation

ForAppending (8): This mode opens the file and moves the cursor to the end, allowing you to add new data without deleting the existing content.

Using this technique, you can log errors or results in a file continuously without worrying about data loss.

Tips for Managing VBScript Text File Operations

  1. Error Handling: Always include error handling, especially when dealing with file paths that may not exist.
  2. Check File Existence: Use the FileExists method to verify if a file is present before reading or writing.
  3. Use Constants: Instead of remembering numeric codes like 1 for reading, create constants to improve code readability.
Example Code with Error Handling
On Error Resume Next ' Enable error handling
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Check if the file exists
If objFSO.FileExists("C:\path\to\yourfile.txt") Then
    Set objFile = objFSO.OpenTextFile("C:\path\to\yourfile.txt", 1)
    Do Until objFile.AtEndOfStream
        strLine = objFile.ReadLine
        WScript.Echo strLine
    Loop
    objFile.Close
Else
    WScript.Echo "File does not exist."
End If
On Error GoTo 0 ' Disable error handling
'read write text files using vbscript

For more best practices in VBScript, you can also refer to Microsoft’s official guide on VBScript best practices.

Conclusion

With these techniques, you can confidently read write text files using VBScript. VBScript provides a simple yet powerful way to automate file tasks on Windows systems, making it an essential tool for administrators and developers alike.

For further learning, explore tutorials on handling more complex file operations with VBScript, including copying, deleting, and moving files.

One more thing. You must be seeing We have used WScript.echo to display data. You can use msgbox as well but It will wait for you to close the msgbox whereas, if you use WSCript, it just keeps printing.

Reference

Discover more from Automation Script

Subscribe to get the latest posts sent to your email.

Related Posts