Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - 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 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
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 - Advanced concepts


    (Page 4 of 4 )

    Simply reading a text file is generally not enough.  You will usually need to be able to work with the data you find.  This is called parsing.  Parsing can be an extremely arduous task depending on the type of data you are working with and how you want to manipulate it.

    I want to present a couple of quick advanced techniques that you will probably find useful as well as a couple of tips to keep in mind when developing your own solutions.

    You can only read text files from top to bottom in one pass.  There is no way to move back up through a text file.

    Text files, especially log files, can become very large, very quickly.  If you are processing large amounts of information it can be helpful to know what size your text file is before you begin—more specifically to know that it’s not empty!  Here is a modification to our example that employs a little error checking to make sure our text file actually contains data to prevent Read errors.

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFile = GetFile(“C:\addresses.txt”)

    If objFile.Size > 0 Then

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

       Do Until objReadFile.AtEndOfStream

          strContents = strContents & objFile.Read(1)

       Loop

       Wscript.Echo strContents

       objFile.Close

    Else

       Wscript.Echo “File is empty.”

    End If

    Frequently you will be reading text files for the purpose of finding specific data.  Often this is much easier if we read our text file into an array first.  In this example we are going to return the fourth record from our example text file.

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFile = GetFile(“C:\addresses.txt”)

    If objFile.Size > 0 Then

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

       i = 1

       Do Until objReadFile.AtEndOfStream

           Redim Preserve arrData(i)

           arrData(i) = objFile.ReadLine

           i = i + 1

       Loop

       objFile.Close

       strContents = arrData(4)

    End If

    One of the most common uses for reading a text file is to check logged data.  Frequently that data is updated by appending information to an existing file which means that the newest data is at the bottom.

    If you were reading a text file and wanted to know the most recent action, you would first have to read through the whole file to find the last line.  In cases like this it is much easier to process the file in reverse order.

    Watch how we can modify our array example so that it processes a file one line at a time from the bottom up.

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFile = GetFile(“C:\addresses.txt”)

    If objFile.Size > 0 Then

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

       i = 1

       Do Until objReadFile.AtEndOfStream

           Redim Preserve arrData(i)

           arrData(i) = objFile.ReadLine

           i = i + 1

       Loop

       objFile.Close

       For l = Ubound(arrData) to LBound(arrData) Step -1

           Wscript.Echo “Record”, l, “=”, arrData(l)

       Next

    End If

    That wraps up part one of this three part series.  In the next part I’ll show you how to write to text files.  So, until next time, keep coding!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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

    WINDOWS SCRIPTING ARTICLES

    - Adding Controls to an Application with WPF
    - A Portable Scripting Toolbox
    - WPF Through an Example: Introduction
    - Beginning SharePoint Web Part Development
    - More Alternative Languages for WSH
    - WPF Control Layout
    - WSH in Other Languages
    - Screen Capturing via GDI+ and GDI
    - Understanding Procedures in VBScript
    - Printing Documents in WSH
    - Generating Outlook Signatures Based on Activ...
    - VBScript: Converting and Formatting with Fun...
    - VBScript: Conversion and Format Functions
    - VBScript: Array Functions
    - VBScript: Strings, You Can`t Function withou...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway