Login   /   Register

Read a text file from the bottom up

Rate this article
     0 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 5
Loading ... Loading ...
April 1st, 2008 by daniva

The FileSystemObject is extremely useful, but it also has its limitations; one of the major limitations is the fact that it can only read a file from top to bottom. Unlike, say, the Tail utility, you can’t ask the FileSystemObject to read a file from the bottom up.

But that’s OK; as always seems to be the case with scripting, you can find a way to work around limitations like this. In this case, what we do is go ahead and read the file from top to bottom, beginning with line 1 and ending with line whatever. However, rather than immediately echoing these lines to the screen, we’ll store them in an array, with each line in the file representing one element in the array.

Why do we do that? Well, now we have an array that looks like this, replicating the way information is stored in the text file:

  • Boston
  • Madrid
  • Buenos Aires
  • Jerusalem
  • London
  • Paris
  • Moscow

Admittedly, all we seem to have done so far is re-invent the wheel. However, there’s one important difference between a text file and an array: it’s actually pretty easy to read an array from the bottom to the top. We’re going to tell our script to begin with the last item in the array (which we can determine using the Ubound function) and work backwards towards the first item in the array (Lbound).

In our sample array, we have 7 items in the array; the last item (Ubound) is the word Moscow and the first item (Lbound) is the word Boston. The first item in the array is given an index number of 0; thus Boston has an index number of 0 and Moscow has an index number of 6. Our script is going to start with item 6 and work backwards to item 0. How do we work backwards like that? We add the parameter Step -1, which essentially says, "Look at item 6 and do something with it. Then subtract 1 from the index number, which leaves 5. Look at item 5, and do something with it. Repeat this process until you’ve looked at all the items in the array." Here’s what the code looks like:

Const ForReading = 1
Dim fileLinesArr()
i = 0
Set fso = CreateObject( "Scripting.FileSystemObject" )
Set textFile = fso.OpenTextFile( "C:\Cities.txt", ForReading )
Do Until textFile.AtEndOfStream
     Redim Preserve fileLinesArr( i )
     fileLinesArr( i ) = textFile.ReadLine
     i = i + 1
Loop
textFile.Close : Set textFile = Nothing
For i = Ubound( fileLinesArr ) To LBound( fileLinesArr ) Step -1
    Print fileLinesArr( i )
Next

Slightly confusing if you haven’t worked much with arrays, but once you get the hang of it you’ll see how simple it really is.

Based on "Can I Read a Text File From the Bottom Up?" from the Scripting Guys

Posted in VBScript

Leave a Reply

You must be logged in to post a comment.

This article was viewed 373 times