Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 - Opening an existing text file


    (Page 2 of 4 )

    Now let’s take a look at opening an existing text file.  The file system object provides the OpenTextFile method.  The OpenTextFile method returns a TextStream object so don’t forget to use a Set statement when connecting.  The OpenTextFile method uses a simple syntax.

    object.OpenTextFile Filename [, Iomode[, Create[, Format]]]

    Filename is a full path name to the file we would like to open.  Imode takes a Constant value listed in Table 1.  Its default is ForReading.  Create is a Boolean value.  True will create the text file if it doesn’t exist.  Its default, False, will generate an error if the file does not exist.  Finally, Format is a Tristate constant as listed in Table 2.  It defaults to TristateFalse.  ASCII is generally acceptable for all purposes.  If you’re unsure, just leave it at the default.

    Table 1: Constant Values for Iomode Attribute

    Constant

    Value

    Description

    ForReading

    1

    Read-Only

    ForWriting

    2

    Allow writing, overwrite contents

    ForAppending

    8

    Allow writing, append to file rather than overwrite

     

    Table 2: Tristate Values for Format Attribute

    Constant

    Value

    Description

    TristateTrue

    -1

    Unicode

    TristateFalse

    0

    ASCII

    TristateUseDefault

    -2

    Use system default

    Be careful using the ForWriting constant as it does overwrite any existing information.  If you wish to edit a file: read its contents first, make any changes, and then write it back in its entirety.

    We’re going to begin our code by connecting to the FileSystemObject once again.  Then we’re going to tell it to connect to our example text file and create it if it doesn’t exist.  Let’s take a look at the code.

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFSO.OpenTextFile("C:\ScriptLog.txt", 2, 1)

    objFile.Write("Hello World")

    objFile.Close

    This simple snippet writes a single line to our text file and then closes the data stream.  If you open the C:\ScriptLog.txt file in a text edit such as Notepad you should see the following result.

    Hello World

    We make use of the TextStream object’s Write method to write a string to our TextStream object.  The syntax for the Write object is shown below.  Now let’s run our example code again and write a few more strings to our file.

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFSO.OpenTextFile("C:\ScriptLog.txt", 2, 1)

    objFile.Write("Hello World")

    objFile.Write("This is a test")

    objFile.Close

    Now if we examine our file we should find that it contains the following text.

    Hello WorldThis is a test

    As you can see, the Write method simply writes our string to the file pointer’s current position in the file.  We never told it to write to a new line so it just appended our second string to the first.  There are two ways we can separate this to another line.  We’ll look at both in our next example, but first let’s take a look at the syntax for our write methods.

    object.Write(string)

    object.WriteLine([string])

    The write method has a single required parameter and will write the supplied string to a file at the current file pointer position.  The WriteLine method will also write to the current file pointer position, but will add a newline character to the end of the supplied string.  The string parameter for the WriteLine method is optional and if omitted it will simply write a single newline character.

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFSO.OpenTextFile("C:\ScriptLog.txt", 2, 1)

    objFile.Write("Hello World")

    objFile.Write(vbCrLf)

    objFile.WriteLine("This is a test")

    objFile.Write("This text should be on a new line")

    objFile.WriteLine

    objFile.WriteLine("As should this")

    objFile.Close

    Our result should show each string on a new line.  We’ve used several different methods in this code to make that happen.

    Hello World

    This is a test

    This text should be on a new line

    As should this

    ß Note the blank line

    The first two Write lines work together.  The first wrote our string but left the file pointer on same line.  The second Write line was used to manually write a line ending character.  In the third line the WriteLine method was used to write a string complete with a linefeed.

    Moving on, we used the Write method and the WriteLine method together.  The Write method wrote “This text should be on a new line” to our file.  Then we used the WriteLine method without any parameters to add a linefeed and move the file pointer to a new line.

    Finally, in the last line we used the WriteLine method to write “As should this” to our file.  Remember that the WriteLine method always ends with a newline character.  That’s why our text file contained a blank line at the end.

    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

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT