Use Vbscript to read a text file from the back to the front

Source: Internet
Author: User

Q:
Hi, scripting guy! I have a log file. New data is always added to the bottom of the file. That is to say, 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

A:
Hello, MB. FileSystemObject is very useful, but it also has its own restrictions. One of the main restrictions is that it can only read the file content from and to the back. Unlike the tail tool, you cannot require FileSystemObject to read files from the back and forward. (Well, we assume you want to do this, but it cannot be implemented for you .)

However, it doesn't matter. Just like using scripts to do other things, you can always find a way to break through these limitations. In this case, what we do is to continue to read the file from the front to the back, from the first row or to the last row. However, the vertical lines are different 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? Well, now we have an array like below, saving the information in the text file:

Viotlet
Indigo
Blue
Green
Yellow
Orange
Red

It is undeniable that what we are doing now is just like re-inventing the wheel. However, there is an important difference between text files and arrays: it is very easy to read arrays from the back and forward. We will start the script from the last project in the array (we can use the ubound function to determine) and read from the back to the first project (lbound) of the array ).

In our example array, there are 7 projects. The last project (ubound) is the word red, and the first project (lbound) is the word VILET. The subscript of the first item in the array is 0. Therefore, the subscript of viletting is 0 and that of red is 6. Our script starts from Project 6 and ends with project 0. So how to implement it? We set the parameter step to-1, which means, "locate Project 6, then do something for it; then subtract the subscript by 1 and get 5. Find Project 5 and then do something to it. Repeat this process until all the items in the array are processed ."

ImplementationCodeAs follows:

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 have never used an array, it may seem a bit confusing, but after you are familiar with it, you will find it actually very simple.

Well, the output result of reading the file from the back and forward is as follows:

Red orange yellow green blue indigo Violet
For more information about array usage, see the Microsoft Windows 2000 script writing 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.