Counting files / difficulty with gettoken()

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Papoulka
Posts: 455
Joined: 13 Jul 2013 23:41

Counting files / difficulty with gettoken()

Post by Papoulka »

I want to recursively count the number of ".dat" files in a folder structure. I hoped there might be a direct way in some folderreport variant but haven't found it if so. I also hoped someone might have posted a script for this by now but don't find that either.

Note that I prefer to exclude files such as "january.dat.dummy" i.e. I want to count only real extensions.

I am trying variations on this basic idea:

Code: Select all

$filelist = folderreport("files", "r", , "r", , "|");
  echo gettokenindex(".dat", "$filelist", , "wc");
The above works but will include dummy files as shown above. So I tried adding a trailing delimiter and then searching for ".dat|":

Code: Select all

$filelist = folderreport("files", "r", , "r", , "|")."|";
  echo gettokenindex(".dat|", "$filelist", , "wc");
but this always returns 0, even if I try a simple delimiter such as "_" instead of "|". That really puzzles me.

Also, every variation returns 0 without the 'w' flag - but according to the Help the absence of this flag should force the token to be treated literally, which is what I think I need.

Any ideas?

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

Re: Counting files / difficulty with gettoken()

Post by highend »

Code: Select all

$filelist = folderreport("files", "r", , "r", , "|");
    text gettoken(formatlist($filelist, "f", "|", "*.dat", "f") , "count", "|");
And the same could be done with a regexmatches command...
One of my scripts helped you out? Please donate via Paypal

Papoulka
Posts: 455
Joined: 13 Jul 2013 23:41

Re: Counting files / difficulty with gettoken()

Post by Papoulka »

Thank you highend. That works perfectly to recursively count all files with a given extension.

Anyone...

Why did my script not work? I suspect an undocumented limitation in gettokenindex() - or is it a bug?

And why would the 'w' flag be needed for a literal search?

klownboy
Posts: 4462
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Re: Counting files / difficulty with gettoken()

Post by klownboy »

Hi Papoulka, try this with the wildcard for the name (e.g., *.dat). It works.

Code: Select all

 $filelist = folderreport("files", "r", ,"r", , "|");
  echo gettokenindex("*.dat", "$filelist", , "wc");
Specifying ".dat" as stated in help should be the same as specifying *.dat* since no wildcards are included yet we use wildcard flag "w".
w: Token has wildcards.
If missing then token is treated literally.
If included and token does not contain wildcards treat it as *token*.
So this seems to work as well, but since it is essentially looking for *.dat*", if you run it with XYplorer as the current folder you'll get all the *.dat2 files in the "Thumbnails" folder as well. Whereas, if you run the upper version from XYplorer, you will only get the files with a .dat extension.

Code: Select all

 $filelist = folderreport("files", "r", ,"r", , "|");
  echo gettokenindex(".dat", "$filelist", , "wc");
The upper version doesn't include files like "file1.dat.dummy" in the count total. Whereas the 2nd version will. Let me know if you are having an issue.

Papoulka
Posts: 455
Joined: 13 Jul 2013 23:41

Re: Counting files / difficulty with gettoken()

Post by Papoulka »

Thank you, klownboy.

Your first version:

Code: Select all

 $filelist = folderreport("files", "r", ,"r", , "|");
  echo gettokenindex("*.dat", "$filelist", , "wc");
also works perfectly well in my tests, recursively finding and counting only genuine ".dat" files.

I appreciate the thoughtful replies.

Post Reply