Home > Windows Server News > Scripting School: Writing output to a text file
Windows Server News:
EMAIL THIS

Scripting School: Writing output to a text file

By Christa Anderson, Contributor
12 Dec 2005 | SearchWinSystems.com

Expert advice on Windows-based systems and hardware
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google

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:

  1. Create an instance of the file system object.
  2. Create a text file from the file system object.
  3. Get the path for the new text file.
  4. Create the file at the path location.
  5. Write to the file.
  6. Close the file.
  7. 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.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Windows Server Management - Virtualization, Consolidation, Clustering
HomeTopicsBlogsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2004 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts