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  
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

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

    - 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 5 hosted by Hostway
    Stay green...Green IT