Use VBScript to read a text file from back to front _vbs

Source: Internet
Author: User
Ask:
Hey, scripting guy!. I have a log file where the new data is always added to the bottom of the file, which means that the most recent entries are always at the end of the file. I want to read the content from the last line until the first line of the file, but I don't know how to implement it.

--MB, Milwaukee, WI

For:
Hello, MB. FileSystemObject is very useful, but it also has some limitations of its own; one of the main limitations is that it can only read the contents of the file from the back. Unlike the Tail tool, you cannot require FileSystemObject to read files from the back forward. (OK, let's assume you're asking, but it's definitely not going to work for you.) )

But, it doesn't matter, just as using a script to do something else, you can always find a way to break through this constraint. In this case, all we do is go ahead and read the file backwards, from the first line to the end of the line. However, and immediately display these lines differently on the screen, we save them in an array, and each row in the file represents an element in the array.

Why are we doing this? OK, now we have an array of the following, which holds the information in the text file:

Violet
Indigo
Blue
Green
Yellow
Orange
Red

There is no denying that what we are doing now is like reinventing the wheel. However, there is an important difference between the text files and the arrays: it is easy to read the array backwards. We'll start the script from the last item in the array (we can use the Ubound function to determine) and read forward to the first item (Lbound) of the array.

In our sample array, there are 7 items; the last item (Ubound) is the word red, and the first item (Lbound) is the word violet. The subscript for the first item in the array is 0, so the subscript for the Violet is the 0,red subscript 6. Our script will proceed from Project 6 to project 0. How does that happen? We set the parameter step to-1, which means, "Find item 6 and then do something about it; then subtract the subscript by 1 and get 5." When you find item 5, do something about it. Repeat this process until you have finished processing all the items in the array. ”

The specific implementation code looks like this:

Dim Arrfilelines ()
i = 0
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objfile = objFSO.OpenTextFile ("C:\FSO\ScriptLog.txt", 1)
Do Until Objfile.atendofstream
Redim Preserve Arrfilelines (i)
Arrfilelines (i) = Objfile.readline
i = i + 1
Loop
Objfile.close
For L = Ubound (arrfilelines) to LBound (Arrfilelines) Step-1
WScript.Echo Arrfilelines (L)
Next

If you don't use a few groups, you may look a bit confused, but after you get to know it, you'll find it's actually very simple.

OK, we read the output from the back of the file as follows:

Red orange yellow green blue indigo Violet
For more information about array use, see the Microsoft Windows 2000 Scripting Guide.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.