Windows Scripting
  Home arrow Windows Scripting arrow Writing Text Files in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Writing Text Files in WSH
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-01-24

    Table of Contents:
  • Writing Text Files in WSH
  • Opening an existing text file
  • Advanced Writing Methods
  • Advanced writing concepts

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Writing Text Files in WSH


    (Page 1 of 4 )

    Being able to write to text files can give your script a very powerful feature set.  Among other things this will enable you to edit many program configurations and system settings, log a script’s output, or create other customized scripts on the fly.  Please take the time to read the first article in this series in which I covered how to read text files in WSH.

    By far the most common use of writing text files is for logging purposes.  We will focus mostly on this application; however, you can easily use the methods provided here for any other purpose that involves writing to a plain text file.

    Any time you want to manipulate files in WSH you should automatically think of the FileSystemObject.  We’ll begin our script by making a call to its namespace.  Familiarize yourself with the FileSystemObject because it is a commonplace in many scripts.

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFSO.CreateTextFile("C:\ScriptLog.txt")

    The next step is to create a text file.  This is done with the FileSystemObject’s CreateTextFile method.  The CreateTextFile method returns an object so we’ll need to use the Set command.  To use the CreateTextFile method we must supply it with a file path and name.

    That’s the simplest way, but suppose we were doing some more intricate coding.  Maybe we’re using dynamic file paths.  The following alternative uses a few more methods to create a text file that allows for a little more flexibility and control.

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    strPath = "C:\path"

    strFileName = objFSO.GetTempName

    strFile = objFSO.BuildPath(strPath, strFileName)

     

    Set objFile = objFSO.CreateTextFile(strFile)

    objFile.Close

    objFSO.DeleteFile(strFile)

    This code snippet introduces a lot of methods.  Let’s take a look at their syntax and then we’ll discuss what the code does.

    object.CreateTextFile Name[, Overwrite[, Unicode]]

    object.BuildPath Path, Filename

    object.DeleteFile Name[, Force]

    Now, let’s go back to our code snippet.  We begin by connecting to the FileSystemObject.  Next, we assign a path to the strPath string variable.  We use the GetTempName method to assign a random name to the strFileName string variable.  Now that we have a path and a file name, we use the BuildPath method to combine the two into a full path name which we assign to the strFile string variable.  Still with me?

    Now we can create the file.  We use the CreateTextFile method like we did in our first example.  Let’s take a second and talk about its syntax.  The only required attribute is a full path to the file that we want created.  However, there are a few optional attributes.  Overwrite and Unicode are both Boolean attributes that determine whether we should overwrite any existing file (default is True) and whether or not to use Unicode (default is False, meaning to use ASCII) character set, respectively.

    Using the Close method to disconnect from a file object will automatically save or discard any changes based on the Iomode used to open it.

    After creating the text file, we have to use the Close method to release it before we can delete it with the DeleteFile method.  Why close it?  The file object is automatically opened for editing when it is created.  If we don’t close it first we’re likely to run into some errors when we try to delete it while it’s in use.  It’s also good to note that the DeleteFile method has an additional Force attribute that is a Boolean value.  The Force attribute allows us to force deletion of Read-only files.

    More Windows Scripting Articles
    More By Nilpo/Developer Shed Staff Writer


       · In part 2 of this series, we learn how to write information to text files. I'd love...
       · the link for the file does not work. you get a page not found error.
     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 9 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek