Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - Reading 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

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

    Table of Contents:
  • Reading Text Files in WSH
  • Reading methods
  • More methods
  • Advanced 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


    Reading Text Files in WSH - Reading methods


    (Page 2 of 4 )

    Any time you start thinking about working with files, you should automatically be thinking of WSH’s FileSystemObject.  This is WSH’s way of allowing us to work with files and folders.  So, naturally, we’ll begin our script by connecting to the FileSystemObject.

    Set objFSO = CreateObject("Scripting.FileSystemObject")

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

    Do Until objFile.AtEndOfStream

       strContents = strContents & objFile.Read(1)

    Loop

    Wscript.Echo strContents

    objFile.Close

    This simple code snippet opens a text file and reads it to the end one character at a time.  Let’s take a look at the methods we used.

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

    object.Read(Int)

    In the second line, the OpenTextFile method opens the text file ForReading and returns it as a TextStream object.  The second parameter accepts an Iomode constant depending on how we want to open the file.  The default is ForReading.

    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

    Next, set up a loop to move through the text file.  The AtEndOfStream property tells our script when it reaches the end of the text stream.  The Read method is used to read the contents of the text file one character at a time.

    The Read method returns a string containing the characters read from a text file.  It accepts a single integer for a parameter indicating how many characters to read.

    The TextStream Object’s Read method will return all characters it finds including whitespace characters such as carriage returns and line feeds.

    The example script ends by Echoing back the entire text file as one string.  Finally, the Close method is used to close the text stream.

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


       · I continue to explore the world of WSH with the first of a 3-part series dealing...
     

    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-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT