Scripting School: Writing output to a file

One problem with producing output from a script is that it's temporary. Here's how you can make your scripts write the output to a text file using the File System Object.

Being able to write text to a file is nice, but it's not terribly useful most of the time. You need to be able to take the output that you might echo to the screen and stick it into the text file. Luckily, anything that you can echo to the screen can be included in a text file, with or without accompanying hard-coded text.

For example, the previous code would work just as well as this example in which sUser is the name of the currently logged-in user, gathered earlier in the script and assigned to a variable:

oFiletxt.WriteLine(sUser)

oFiletxt.WriteLine("and this is some text")

If you can echo output to the screen, you can output it to a text file and save that file on your computer.

The WriteLine method can handle more than one piece of information in its arguments -- that is, you can include both variables and hard-coded text in the same line. However, commas have a specific meaning to that method, and you can't separate pieces of the lines with commas. You can only do it with ampersands. In addition, because you're using ampersands, you must manually add spaces where appropriate since the spaces that commas create in Wscript.Echo will not be provided. One example of a line to create mixed output could look like this:

oFiletxt.WriteLine("the user is "&sUser&sNumber)

You can also use operators as arguments to WriteLine, whether dealing with hard-coded numbers or variables. For example, if the value of sNumber is 1000, the following line will write a value of 200 to the text file.

oFiletxt.WriteLine(sNumber/5)

To time-stamp the information that you are writing to the file, be sure to use the Now function, like this:

oFiletxt.WriteLine("This inventory was taken at "&Now)

This will create a line in the file that looks like this:

This inventory was taken at 11/28/2005 6:48:26 PM


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
When Christa Anderson began working with Windows Server operating systems in 1992, she became increasingly interested in finding more efficient and flexible ways of performing routine tasks. Christa has written extensively about administrative scripting and taught technical sessions on the subject at conferences such as Comdex and CeBIT, helping people who had never done any scripting to write their own scripts in half a day. In addition to her interest in scripting Windows management, Christa is an authority on server-based computing and the program manager for Terminal Services licensing in Longhorn. If you have a scripting question for Christa, please e-mail her at scripting@SearchWinSystems.com.

Dig deeper on Windows Server Clustering

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

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

Your password has been sent to:

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchWinIT

SearchEnterpriseDesktop

SearchVirtualDesktop

Close