Page 1 of 1

XYplorer hangs on script execution

Posted: 27 Mar 2016 15:06
by zakhar
Hello all!

XYplorer hangs on execution of the script of the following content:

(XYplorer must
- find *.jpg- and *.txt-files,
- calculate their number and length,
- write these data into a file.
Hangs occurs if the number of file is over 4500.
If the amount of files is under 4500 XYplorer does not hang and works properly.)

Code: Select all

 $dateandtimeofstart = <date yyyy.mm.dd hh.nn.ss>;

 $dateforfile = gettoken ("$dateandtimeofstart",1," ",,);
 $timeforfile = gettoken ("$dateandtimeofstart",-1," ",,);
 $dateandtimeforfilename = "$dateforfile-$timeforfile";

 $folder = "C:\folder\";

 $searchresultjpg = quicksearch ("*.jpg /f", "$folder",<crlf>);
 $filecountjpg = gettoken ("$searchresultjpg","count",<crlf>,,);

 $i1 = 1;
 while ($i1 < "$filecountjpg" + 1) {

 $line = gettoken ("$searchresultjpg",$i1,<crlf>);
 $filesize = filesize ($line);
 $sumsizejpg = "$sumsizejpg"+"$filesize";

 $i1++;
 }

 $searchresulttxt = quicksearch ("*.txt /f", "$folder",<crlf>);
 $filecounttxt = gettoken ("$searchresulttxt","count",<crlf>,,);

 $i2 = 1;
 while ($i2 < "$filecounttxt" + 1) {

 $line = gettoken ("$searchresulttxt",$i2,<crlf>);
 $filesize = filesize ($line);
 $sumsizetxt = "$sumsizetxt"+"$filesize";

 $i2++;
 }

 writefile("C:\folder-1\$dateandtimeforfilename-files.txt","$folder<crlf>*.jpg $filecountjpg $sumsizejpg<crlf>*.txt $filecounttxt $sumsizetxt<crlf><crlf>",a,t);

 $dateandtimeofend = <date yyyy.mm.dd hh.nn.ss>;

 writefile("C:\folder-1\$dateandtimeforfilename-files.txt",">-$dateandtimeofstart<crlf>->$dateandtimeofend<crlf><crlf>",a,t);

 msg Script executed!;

Is there any way to avoid these hangs?
Should the sum of the file lengths be calculated in this script in an another way?

In the Info Panel the files are found very quickly and the sum of the lengths of the founded files is calculated automatically, very quickly, without problems.
It should be possible to get the sum of file lengths without hangs.

Re: XYplorer hangs on script execution

Posted: 27 Mar 2016 18:51
by PeterH
At least for test: try, in both loops
$filesize = filesize ($line) / 1024; // size in kb
If it hangs much later you have reached a limit here.

And you should set $sumsizejpg and $sumsizetxt to 0 before looping. Though it seems that an uninitialized variable is trated as 0 in an arithmetic operation. I'm always cautious here...

By the way:
$sumsizejpg = "$sumsizejpg"+"$filesize";
tells to treat the vars as strings before adding them (by converting them to numbers).
$sumsizejpg = $sumsizejpg + $filesize;
just adds the contents of the variables.

Re: XYplorer hangs on script execution

Posted: 27 Mar 2016 20:27
by zakhar
Adding /1024, predeclaring $'s = 0 and removing of "" seem not to help. :cry:
If it hangs much later you have reached a limit here.
It is interesting to know what kind of limit do I reach here. (?)

Re: XYplorer hangs on script execution

Posted: 27 Mar 2016 20:29
by highend
You could use a folderreport() to get the necessary information (although this takes much longer than a quicksearch() on large folders) and then grab the necessary stuff via regexmatch and sum it up (via eval()). That would let you get rid of the while loops to do that stuff...

Re: XYplorer hangs on script execution

Posted: 27 Mar 2016 22:16
by zakhar
highend wrote:grab the necessary stuff via regexmatch
I will not be able to define the regular expression for regexpmatches to extract the numbers from the file or variable created by folderreport
as the regular expression syntax needed for regexpmatches is not documented enough in the help file of XYplorer (or I did not find needed information.). :cry:
Please point me to the needed information.

Re: XYplorer hangs on script execution

Posted: 28 Mar 2016 01:37
by highend

Code: Select all

$files = folderreport("files:{size raw}+|{name}", "r", "<curitem>", "r", , "<crlf>");
$txtFiles = formatlist($files, "f", "<crlf>", "*.txt", "f");
regexreplace(regexreplace($txtFiles, "^(.*?)\|(.*$)", "$1"), "\r?\n")
Be careful, eval() has problems with larger stack sizes as well so you'll need a while loop that uses eval in blocks (e.g. 100 at a time)

I guess it's better to offload this procedure to a less limited language (e.g. .ahk)...

Re: XYplorer hangs on script execution

Posted: 28 Mar 2016 14:22
by zakhar
Thank you, highend! I will try to implement your code.