Reading a text file from end to beginning.

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
EnderFFX
Pro Scripter
Posts: 92
Joined: Mon Mar 08, 2004 6:17 am

Reading a text file from end to beginning.

Post by EnderFFX » Wed Dec 17, 2008 3:14 pm

I'd like to read a text file from end to beginning, or from the end to some arbitrary point in the text file. Anyone have any ideas on how I could do this without reading the entire file?

I'm just looking for logic, not asking for someone to write the code for me. (but i would accept it if someone had it laying around. :D )

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Dec 17, 2008 4:02 pm

Use the method posted here to get the line number of the last line then use the ReadLn function using the last line as the line to read. Subtract one from the last line and you have the next to last line, etc.

EnderFFX
Pro Scripter
Posts: 92
Joined: Mon Mar 08, 2004 6:17 am

Post by EnderFFX » Wed Dec 17, 2008 4:04 pm

JRL wrote:Use the method posted here to get the line number of the last line then use the ReadLn function using the last line as the line to read. Subtract one from the last line and you have the next to last line, etc.
That's exactly what I'm looking for. I'll give it a try when I get home from work! Thank you.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Dec 17, 2008 4:18 pm

Or:

Code: Select all

ReadFile>c:\somefile.txt,file_data
Separate>file_data,CRLF,lines
If>lines_count>0
Let>k=lines_count
Repeat>k
    Let>this_line=lines_%k%
    MessageModal>this_line
    Let>k=k-1
Until>k=0
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

EnderFFX
Pro Scripter
Posts: 92
Joined: Mon Mar 08, 2004 6:17 am

Post by EnderFFX » Wed Dec 17, 2008 7:44 pm

mtettmar wrote:Or:

Code: Select all

ReadFile>c:\somefile.txt,file_data
Separate>file_data,CRLF,lines
If>lines_count>0
Let>k=lines_count
Repeat>k
    Let>this_line=lines_%k%
    MessageModal>this_line
    Let>k=k-1
Until>k=0
I was considering this method as well, but here was my question. I would be doing this with around 16-20 files at a time (one right after another) and the files could get as large as 1000-2000 lines a piece. (worst case scenario) could this cause a memory problem?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Dec 17, 2008 8:09 pm

No.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts