Column question: Lines of text in a text file?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
kunkel321
Posts: 663
Joined: 10 Jun 2012 03:45
Location: Near Seattle

Column question: Lines of text in a text file?

Post by kunkel321 »

Is there a straight-forward way to show this?

What I'd really like is the number of lines of code in an ahk file. I don't suspect there is an easy way to do that, but I believe that ahk files are just text files with UTF-8 encoding. So maybe the overall number of lines(?)

I have a Custom Column that uses

Code: Select all

<prop *Pages> Pgs.
for the number of pages in a pdf or doc file, but it doesn't seem to work for txt or ahk.
ste(phen|ve) kunkel
Scaling: Main monitor 125%, Secondary monitor on the right 100%
OS: Win 10. XYplorer version: Latest beta, unless specified.

highend
Posts: 14672
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Column question: Lines of text in a text file?

Post by highend »

return gettoken(readfile(<cc_item>), "count", <crlf>);
One of my scripts helped you out? Please donate via Paypal

kunkel321
Posts: 663
Joined: 10 Jun 2012 03:45
Location: Near Seattle

Re: Column question: Lines of text in a text file?

Post by kunkel321 »

highend wrote: 08 Jan 2026 19:01 return gettoken(readfile(<cc_item>), "count", <crlf>);
Thanks Highend! Below is the setup, if anyone is curious.
Also a follow up question: Is it possible to append a bit of text to the end, when there is a value returned? So that it will show up as, for example
234 Lns.
?
CC 2026-01-08_14-11-48.jpg
CC 2026-01-08_14-11-48.jpg (78.6 KiB) Viewed 415 times
ste(phen|ve) kunkel
Scaling: Main monitor 125%, Secondary monitor on the right 100%
OS: Win 10. XYplorer version: Latest beta, unless specified.

highend
Posts: 14672
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Column question: Lines of text in a text file?

Post by highend »

Concatenate it?
. " Lns.";
One of my scripts helped you out? Please donate via Paypal

kunkel321
Posts: 663
Joined: 10 Jun 2012 03:45
Location: Near Seattle

Re: Column question: Lines of text in a text file?

Post by kunkel321 »

highend wrote: 08 Jan 2026 23:25
Ah yes. I was missing the dot. This does the trick.
return gettoken(readfile(<cc_item>), "count", <crlf>) . " Lns." ;

Interestingly, I'm finding that it doesn't work on every txt file. On some of them, and all ahk files only ever show "1".

I'm guessing that the <crlf> is the thing being counted? Maybe different types of encoding on the files(?)

<lf> seems to be a little better, but the number of lines returned on the ahk files is still a bit low...
It's also noteworthy that I have this as just one of several components in a Mixed Column... Maybe the next calculation in the mixed column steps happen before the counts are able to finish(?) I don't think that it's a timing issue, as the same (slightly low) numbers appear after each refresh.
ste(phen|ve) kunkel
Scaling: Main monitor 125%, Secondary monitor on the right 100%
OS: Win 10. XYplorer version: Latest beta, unless specified.

highend
Posts: 14672
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Column question: Lines of text in a text file?

Post by highend »

Probably mixed line endings...

Try this cli tool:
Count lines.zip
(17.69 KiB) Downloaded 9 times
Adapt the <path>^^
return runret(lax("<path>\Count lines.exe" "<cc_item>"));
One of my scripts helped you out? Please donate via Paypal

jupe
Posts: 3323
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: Column question: Lines of text in a text file?

Post by jupe »

You could alternatively do it like this natively without the exe, I'd assume it would be faster.

Code: Select all

  $c = readfile(<cc_item>);
  if ($c) {
    $c = replace($c, <crlf>, <lf>);
    $c = replace($c, <lf>, chr(13));
    $c = gettoken($c, "count", chr(13),, 4);
    return "$c Lns.";
  }
@highend: that exe seems to double the line count for UTF-16 files

highend
Posts: 14672
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Column question: Lines of text in a text file?

Post by highend »

You're right, jupe

This version should fix it:
Count lines_v0.2.zip
(16.5 KiB) Downloaded 12 times
It requires SSE2, so almost all processors since 2005 should be able to use it (Pentium 4+)

Tested on UTF-8 (BOM & no BOM), UTF-16 LE (BOM & no BOM), UTF-16 BE (BOM & no BOM)

Your XY version fails on UTF-16 BE without BOM btw, instead of counting 5 lines for a mixed-lines-ending file it counts 9 instead.

@kunkel
For this v0.2 version you need to call it differently:

Code: Select all

return runret(lax("<path>\Count lines.exe" "<cc_item>"), 3:=2);
One of my scripts helped you out? Please donate via Paypal

jupe
Posts: 3323
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: Column question: Lines of text in a text file?

Post by jupe »

Yeah UTF-16BE without BOM are pretty rare though, I guess if you didn't mind slowing down the script slightly, something like this should work in those edge cases too,

Code: Select all

  $r = filetype(<cc_item>) == "Binary" ? 1201 : "";
  $c = readfile(<cc_item>, 3:=$r);
  if ($c) {
    $c = replace($c, <crlf>, <lf>);
    $c = replace($c, <lf>, chr(13));
    $c = gettoken($c, "count", chr(13),, 4);
    return "$c Lns.";
  }
I haven't thoroughly tested it though.

kunkel321
Posts: 663
Joined: 10 Jun 2012 03:45
Location: Near Seattle

Re: Column question: Lines of text in a text file?

Post by kunkel321 »

Cool -- Thanks Fellas! Jupe's first one does indeed seem to return the correct numbers for the txt and ahk files I'm working with. I'm curious though, when using the Count lines.exe file, what folder does the exe go in?
ste(phen|ve) kunkel
Scaling: Main monitor 125%, Secondary monitor on the right 100%
OS: Win 10. XYplorer version: Latest beta, unless specified.

highend
Posts: 14672
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Column question: Lines of text in a text file?

Post by highend »

Wherever you like :)
One of my scripts helped you out? Please donate via Paypal

kunkel321
Posts: 663
Joined: 10 Jun 2012 03:45
Location: Near Seattle

Re: Column question: Lines of text in a text file?

Post by kunkel321 »

Ah yes. Now I see.
ste(phen|ve) kunkel
Scaling: Main monitor 125%, Secondary monitor on the right 100%
OS: Win 10. XYplorer version: Latest beta, unless specified.

Post Reply