Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Have @LINE return the last line of a file

Aug
2,831
149
I'm currently using a UDF to obtain the last line of a file;

Code:
function LastLine=`%@line[%1,%@lines[%1]]`

In the @WORD function,
you can specify the rightmost word by setting n to -0.

I'd like to suggest the following for the @LINE function;
Code:
set LastLine=%@line[%temp\QResults.txt,-0]
...to obtain the last line in a file.

Ref: @WORD TCC internal variable function
Ref: @LINE TCC internal variable function

Joe
 
That's a good suggestion for @LINE. For the time being, you could roll your own (with no safeguards).

Code:
v:\> function nthline `%@line[%1,%@if[%@left[1,%2] == -,%@eval[%@lines[%1]+%2],%2]]`

v:\> (do i=1 to 10 (echo %i)) > 1to10.txt

v:\> echo %@nthline[1to10.txt,0]
1

v:\> echo %@nthline[1to10.txt,1]
2

v:\> echo %@nthline[1to10.txt,-0]
10

v:\> echo %@nthline[1to10.txt,-1]
9
 
Not sure how I missed this,
but @FLINE was added to the TextUtils plugin on 2026/09/30.

If n is negative,
it counts backward from the last line in the file:
n=-0 returns the last line of the file,
n=-1 returns the next-to-last, and so on.

Thanks @Charles Dye

Ref: TextUtils Plugin

Joe
 
Looks like it never got properly added to the .CHM file, for one thing.

Handling of negative values is a little peculiar. If you try to go back past the beginning of the file, it returns the first line. This happens even if you specify /E and the first line is blank. Didn't seem worth fixing at the time....
 
Back
Top