| View previous topic :: View next topic |
| Author |
Message |
CarlosTheTackle
Joined: 19 Oct 2004 Posts: 102
|
Posted: Sat Mar 05, 2005 3:08 am Post subject: FileReadLastLine |
|
|
I'm trying to write a very light-weight script that needs to retrieve the last line of a text file of about 400 lines long (though it can vary from much less than that up to about 410). As far I can see, I can't retrieve the last line without retrieving the entire file, either through a Loop, Read or a FileRead followed by a StringSplit, which means my script is not as fast and unintrusive as I would like it.
Would it be possible to have a command like FileReadLastLine, [line number from end] or would AHK still have to internally read the whole thing anyway, making this no faster? I guess it comes down to whether it's possible to determine the total number of lines without parsing the whole thing or not.
Just a thought.
Cheers,
C |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3004 Location: Minnesota
|
Posted: Sat Mar 05, 2005 4:03 am Post subject: |
|
|
| Couldn't you just use tail.exe and parse the output? |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10480
|
Posted: Sat Mar 05, 2005 8:26 am Post subject: Re: FileReadLastLine |
|
|
Tail is great for these sort of things, though a file of only 400 lines (unless each line is huge) probably wouldn't benefit that much compared to reading the entire file line by line.
| CarlosTheTackle wrote: | | whether it's possible to determine the total number of lines without parsing the whole thing or not. | Unless something is known about the file in advance (e.g. that every line is exactly 150 characters long), there is no way to know how many lines are in it without scanning the entire file.
However, programs such as tail.exe use random access to retrieve the last N lines of any file without having to scan the entire thing. Tail.exe doesn't have to know how many lines exist because it searches from the end toward the beginning until it finds the number of lines you specified.
In other words, with tail.exe you know you have the last N lines but you can't know their line numbers unless the file always has the same number of lines in it. |
|
| Back to top |
|
 |
CarlosTheTackle
Joined: 19 Oct 2004 Posts: 102
|
Posted: Sat Mar 05, 2005 10:45 am Post subject: |
|
|
| jonny wrote: | | Couldn't you just use tail.exe and parse the output? |
Tail.exe huh. I'll check it out. Thanks
| Chris wrote: | | In other words, with tail.exe you know you have the last N lines but you can't know their line numbers unless the file always has the same number of lines in it. |
Sweet. That's all I need. |
|
| Back to top |
|
 |
|