Scripting School: Writing output to a text file |
 |
By Christa Anderson, Contributor
12 Dec 2005 | SearchWinSystems.com |
 |


|
Editor's note: This is the ninth column in a continuing interactive series on scripting that appears monthly on SearchWinSystems.com. You can send any scripting questions to the author, Christa Anderson, at scripting@searchwincomputing.com.
One of the problems with producing output from a script is that it's very temporary -- close the command window or click "OK" on the dialog box, and it's gone.
But there is a way you can keep that output, and you can actually make your scripts write the output to a text file using the File System Object. As I've explained in earlier columns (please see the column index below), this object represents files, folders or drives on the computer, and you can use it to create and edit files.
Creating and writing a text file requires the following steps:
- Create an instance of the file system object.
- Create a text file from the file system object.
- Get the path for the new text file.
- Create the file at the path location.
- Write to the file.
- Close the file.
- Provide confirmation that the file was created (optional).
The code to do all this looks like the following snippet:
Dim oFilesys, oFiletxt, sFilename, sPath
Set oFilesys = CreateObject("Scripting.FileSystemObject")
Set oFiletxt = oFilesys.CreateTextFile("c:\inventory.txt", True)
sPath = oFilesys.GetAbsolutePathName("c:\inventory.txt")
sFilename = oFilesys.GetFileName(sPath)
oFiletxt.WriteLine("This file holds inventory of some kind.")
oFiletxt.Close'
If oFilesys.FileExists(sPath) Then Wscript.Echo "The drive inventory is stored in",sFilename&"."
If you run this snippet more than once, you'll notice that it overwrites the existing file, rather than incrementing the names for new copies e.g. (inventory(1).txt, inventory(2).text). This may be what you want, or it may be annoying. After all, someone else could have potentially created a totally different inventory.txt file and saved it at the same location -- and this snippet would overwrite it with no warning.
To make the script not overwrite the file automatically, change the parameters of CreateTextFile so that the value of overwriting the file is false. This will not automatically increment the file names, but will produce an error in the script telling you that a file with that name already exists.
Set oFiletxt = oFilesys.CreateTextFile("c:\inventory.txt", False)
While it's an option to check it and see if a file already exists and then provide an alternate name, I don't know of an easy way to make VBScript increment file names, since it sees names as a string, not as a string plus a number. Additionally, when there are multiple inventory files in the same location, and you'd like to be able to find which inventory file stores the information in question, then you can just stop and give the file a more descriptive name.
Another way to get around this is to have the person running the script supply a file name and/or path to the script and use that to create the file object. Or, to make the script more responsive to the situation, you could check for the file's existence with FileExists. If it does, and you expect the script to be run interactively, you can use the MsgBox function to pop up a message box prompting the user for a new filename.

Scripting School: Writing output to a text file

Introduction
Writing output to a file
Using saved output
Summary
Read all of Christa's scripting columns:
April 2005: Beginner's guide to scripting
May 2005: It's time to increase your scripting expertise
June 2005: Connect users to network resources
July 2005: More on connecting to network resources
August 2005: Find objects with Windows Scripting Host
September 2005: Windows Script Host arguments
October 2005: Scripting School: Turning the environment with WshShell
November 2005: Scripting School: Connect scripts to remote computers
| ABOUT THE AUTHOR: |
|
Christa Anderson A Terminal Services MVP, Christa Anderson is the
strategic technology manager for visionapp She formerly was program manager for the Microsoft Terminal Services team. She is an
internationally known authority on scripting, the author of Windows Terminal Services, The Definitive Guide to MetaFrame
XP, and co-author of the book Mastering Windows 2003 Server. If you have a scripting question for Christa, please e-mail her at editor@SearchWincomputing.com. She often uses these emails as fodder for her scripting columns.
|
|
');
// -->
|