Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA read large text file line by line in reverse order

VBA question

There is a large log file (around 500,000 lines), I need to read it line by line in reverse order, i.e. from the last line to the first line. I know I can use FileSystemObject in the Microsoft Scripting Runtime reference, but there is no such option like reverse for ReadLine Method in TextStream

Now, the only way I can think of is like this, has a counter and skip previous lines for each of the line I read, but definitely this is not good enough. Any suggestion code/algo will be much appreciated.

like image 659
Ted Xu Avatar asked Oct 05 '22 13:10

Ted Xu


2 Answers

If your log is a kind of database with field which allows to determine the order (is there a date field or line number field), if so you could try to use ADO solution with SQL query to read the log in reverse order (ORDER BY ... DESC). So, you will be able to read from last to first. Or generally- try to use ADO.

like image 80
Kazimierz Jawor Avatar answered Oct 13 '22 11:10

Kazimierz Jawor


A file is not line based, or even character based, it's just bytes so there is no way to read lines in reverse order in a file. How the text is separated into lines is only determined by where there are line break characters in the text.

You can read lines from the beginning and store them in a rotating buffer, so that you have for example the last 1000 lines in the buffer when you reach the end of the file. That way you have a certain number of lines that you can access from your buffer without having to read the entire file for every single line.

After that you know how many lines there are in the file, so when you need to refill the buffer you can just skip a certain number of lines and read the following lines into the buffer.

like image 42
Guffa Avatar answered Oct 13 '22 11:10

Guffa