The VBScript Network and Systems Administrator's Cafe

Apr 17 2008   8:52PM GMT

Creating text files using the Scripting.FileSystemObject with VBScript

Jerry Lees Jerry Lees Profile: Jerry Lees

Well, I didn’t expect the cat to get out of the bag quite so quickly! (Congratulations go out to Stoinov for getting the correct answer I was thinking of so very quickly!) So while he’s been sleeping during his night, which seems to be my day… I thought I’d throw together a quick “Write a text file” script together for you all. (A little trivia for you all in the United States, that I found hard to believe when I learned it… VBScript, and VB for that matter, is in English regardless of where you are in the world. If you’re on a Japanese system writing VBScript, your using the same language as us folks in the United States. Talk about portable code!)

Back to the matter at hand, this script doesn’t do anything terribly spectacular– by itself. All it does is opens (or creates) a text file named what you call it where you specify. The heart of the script is the WriteTextFile () function, which accepts two pieces of data; a full path to a file name and the data you want to write. That’s it! But—- this function will let you write logs, CSV (Comma Separated Value) files or any other form of text file from any type of script for which you choose to use it!

A quick little note on the access method I hard coded into the script. You  will notice three constants used ForReading, ForWriting, and ForAppending these are there so you could change the mode easily without having to go to the documentation for the OpenTextFile Method of the Scripting.FileSystemObject and figure out what numbers to use to change the access method. you simply just need to change the access method from ForReading on the OpenTextFile line of the script to accomplish this task. An excerpt of the Microsoft documentation for the values and descriptions are listed below, for your reference.

Constant Value Description
ForReading 1 Open a file for reading only. You can’t write to this file.
ForWriting 2 Open a file for writing.
ForAppending 8 Open a file and write to the end of the file.

Notice it has a ForAppending attribute? If you wanted to … say log a series of collected values over a number of calls to the script you would have to change the access method to appending, however, keep in mind that ForAppending assumes the file already exists and you will receive an error if it doesn’t.

So, here is the script to write a file…

Option Explicit
Dim Error

Error = WriteTextFile (“c:\test.txt”, “I created a text file with VBScript!”)
If Error = 0 Then
    WScript.Echo “File Written Correctly.”
    WScript.Echo”Error number ” & Error & ” occurred.”
End If

Function WriteTextFile (OutputFile, Data)
    Dim wrtlog, fso
    ‘these are constants for the OpenTextFile method’s file access modes
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    On Error Resume Next
    Set fso = CreateObject(“Scripting.FileSystemObject”)
    Set wrtlog = fso.OpenTextFile (OutputFile, ForWriting , true)

    WriteTextFile = Err.Number ‘You can use Err.Description here to debug.
End Function

That’s it. Small, but a VERY powerful tool to add to your tool belt in scripting.

As always, this code works perfectly. However, sometimes the formatting of the blog breaks the code if you copy and paste it into your editor. So, if you’d like to not type or troubleshoot any syntax errors due to the copy and paste problems– I’ve provided the code for download, plus example output files  from my final tests for you. You’ll find the code and other files available for download from my website’s ( File Depot under the ITKE Blog Scripts category. Enjoy and happy scripting!

Extra credit: Can you modify this function to accept a third parameter for the access method? I invite you to leave a comment and I’ll let you know if your on the same track as I am. Comments are always welcome!

3  Comments on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.
  • Stoinov
    Function WriteTextFile (OutputFile, Data, AccessMethod) ... Set wrtlog = fso.OpenTextFile (OutputFile, AccessMethod , true) ... End Function
    60 pointsBadges:
  • Jerry Lees
    VERY VERY Close, Stoinov. In fact, I'd say it's accurate. I did notice one gotcha on my access method request in the extra credit. It has to do with the scope of the defined constants.
    5,335 pointsBadges:
  • Stoinov
    I guess you mean that const statement should be put outside the function scope? I thought this too obvious to mention :)
    60 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: