Scripts working only in step mode.

Things you’d like to miss in the future...
Post Reply
tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Scripts working only in step mode.

Post by tiago »

The following code should not be a problem:

$temp = "TMP";
setting "ShowFolderSizeInList", 1;
selfilter "$temp", d;
$size = report("{size}", 1);
$size = replace($size, "[Empty]", "0");
$size = replace($size, "-", "0");

IF (0 == $size) {

delete 0, 0;
end 1, "Temp folder is gone!";

} ELSE (0 != $size) {

$button = confirm("There are files inside that 'TMP' temp folder.<crlf>Total folder size is: $size.<crlf>Proceed deletion?")?"_YES":"_NO";
IF ($button == _YES) { delete 0, 0; status "Temp folder deleted."; end 1; }
ELSE ($button == _NO) { end 1, "Deletion canceled."; }

}

Instead it just doesn't work as expected, unless I step through the script. If I don't step, the script always throws me the confirm message even if the temp folder has nothing or files with size equal to zero, which is wrong, it should just delete the temp folder flawlessly.
In step mode the little report on confirm box displays the exact size of the folder, which does not happens in normal execution.
Weird: if I swap the IF/ELSE statements the default behaviour will be deleting the folder with no prompts.
Weirder: the second IF/ELSE pair works fine.
Sometimes stepping through script wipped off the whole parent folder, which is weirdest.

This one is the worse... it just freezes XYplorer, despite correctly creating the file prior to freezing. Again, if I step through the script, everything runs just fine. What's happening?
Using latest stable trial version on a Windows XP sp 2 machine, but tested on another sp 3 and Vista machines and the same happens. Both scripts behave that way either in "scripting, run script..." or "user, run script".

$prefix = 'Novo Texto - ' . RegExReplace("<curfolder>", '_+', '_') . " - <date dd-mm-yyyy>";
$ext = '.txt';

$suffix = GetKey('PostfixNum','General');
$suffix = RegExReplace("$suffix", '^(.*?[^0]?)(0+)([^0]*)$', "$1<crlf>$2<crlf>$3");
$suffix_pre = GetToken("$suffix", 1, "<crlf>");
$suffix_zed = GetToken("$suffix", 2, "<crlf>");
$suffix_suf = GetToken("$suffix", 3, "<crlf>");
$suffix = '';

$min_lead = StrLen("$suffix_zed");
$i = 0;
while (Exists("<curpath>\$prefix$suffix$ext") != 0) {
$i = $i + 1;
$suffix = "$suffix_pre" . StrRepeat('0', $min_lead - StrLen("$i")) . "$i$suffix_suf";
}

$err = WriteFile("<curpath>\$prefix$suffix$ext", "<crlf><clipboard><crlf><crlf>", 'n');
End($err != 1, "Erro ao criar arquivo:<crlf>$prefix$suffix$ext");
Focus('L');
Sel();
$i = 0;
while (GetInfo('CountSelected') == 0 and $i < 1000000000) {
Sel("[$prefix$suffix$ext]",,1);
$i = $i + 1;
}
Power-hungry user!!!

j_c_hallgren
XY Blog Master
Posts: 5824
Joined: 02 Jan 2006 19:34
Location: So. Chatham MA/Clearwater FL
Contact:

Re: Scripts working only in step mode.

Post by j_c_hallgren »

Hi and welcome to the XY forums!

I'm not a scripter so can't help with that...however, in the interests of forum clarity, would you be kind enough to add "code" tags around your scripts so they can be read easier? You can just select the text in edit mode and use the Code button to apply the tags...thanks!
Still spending WAY TOO much time here! But it's such a pleasure helping XY be a treasure!
(XP on laptop with touchpad and thus NO mouse!) Using latest beta vers when possible.

zer0
Posts: 2673
Joined: 19 Jan 2009 20:11

Re: Scripts working only in step mode.

Post by zer0 »

Code: Select all

$temp = "TMP";
   setting "ShowFolderSizeInList", 1;
   selfilter "$temp", d;
   $size = report("{size}", 1);
   $size = replace($size, "[Empty]", "0");
   $size = replace($size, "-", "0");

   IF (0 == $size) {

   delete 0, 0;
   end 1, "Temp folder is gone!";

   }   ELSE (0 != $size) {

   $button = confirm("There are files inside that 'TMP' temp folder.<crlf>Total folder size is: $size.<crlf>Proceed deletion?")?"_YES":"_NO";
   IF ($button == _YES) { delete 0, 0; status "Temp folder deleted."; end 1; }
   ELSE  ($button == _NO) { end 1, "Deletion canceled."; }

   }
While I am not sure if this is the root cause (no time to investigate, it's Valentine's Day :P ), you don't need to state an expression after ELSE. This is because if an expression stated after IF is false then code within the following parentheses is ignored and the next ELSE block is processed. As expressions stated in IF and ELSE blocks are mutually exclusive, stating it in the ELSE block is redundant.
Weird: if I swap the IF/ELSE statements the default behaviour will be deleting the folder with no prompts.
This isn't weird. ELSE must be the last block in the control structure. If it deletes the folder with no prompts then it's a sign that ELSE expression returned FALSE and IF returned TRUE.
Weirder: the second IF/ELSE pair works fine.
I suspect there is a race conflict, which isn't the case with buttons.
Reporting a bug? Have a wish? Got a question? Use search - View roadmap - FAQs: Forum + XY site
Windows 7/10
Always using the latest stable two-decimal build

admin
Site Admin
Posts: 60567
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Scripts working only in step mode.

Post by admin »

tiago wrote:

Code: Select all

}   ELSE (0 != $size) {
is wrong syntax: it should be:

Code: Select all

}   ELSEIF (0 != $size) {

zer0
Posts: 2673
Joined: 19 Jan 2009 20:11

Re: Scripts working only in step mode.

Post by zer0 »

admin wrote:
tiago wrote:

Code: Select all

}   ELSE (0 != $size) {
is wrong syntax: it should be:

Code: Select all

}   ELSEIF (0 != $size) {
Are you sure? :? In his IF loop, he's asking if $size is equal to zero. If that is TRUE then parenthesised code is executed. Otherwise, ELSE comes into play. As expressions specified in IF and ELSE blocks are opposites -- $size is either equal to zero or it is not -- why use ELSEIF, especially since it's a 2-block script. ELSEIF is usually used in 3 block scripts such as IF -> ELSEIF -> ELSE anyway.
Reporting a bug? Have a wish? Got a question? Use search - View roadmap - FAQs: Forum + XY site
Windows 7/10
Always using the latest stable two-decimal build

admin
Site Admin
Posts: 60567
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Scripts working only in step mode.

Post by admin »

zer0 wrote:
admin wrote:
tiago wrote:

Code: Select all

}   ELSE (0 != $size) {
is wrong syntax: it should be:

Code: Select all

}   ELSEIF (0 != $size) {
Are you sure? :? In his IF loop, he's asking if $size is equal to zero. If that is TRUE then parenthesised code is executed. Otherwise, ELSE comes into play. As expressions specified in IF and ELSE blocks are opposites -- $size is either equal to zero or it is not -- why use ELSEIF, especially since it's a 2-block script. ELSEIF is usually used in 3 block scripts such as IF -> ELSEIF -> ELSE anyway.
What you say it absolutely true. But still

Code: Select all

}   ELSE (0 != $size) {
is wrong syntax. It will lead to unexpected results because the expression after else is ignored:

Code: Select all

// will show "2"
  if (1==0) {
    echo "1"
  } else (1==0) {
    echo "2"
  }
I'm not sure if that was causing the problems of the OP. It's just a mistake that I spotted. There might be other mistakes...

PeterH
Posts: 2785
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Scripts working only in step mode.

Post by PeterH »

No doubt:

Code: Select all

}   ELSE (0 != $size) {
is wrong syntax.

But wrong syntax always leads to unexpected results. (I think there can be no expected results for wrong sysntax - then it wouldn't be wrong...)

So the best would be to stop the script and show some message in such a situation.
(I think ELSE must be followed by {...} or maybe by end-of-statement - so it can be recognized.)

I've seen some rather complex scripts here - and reporting recognizable scripting-errors to the script-"programmer" could help a lot on debugging, I think.
Win11 Pro 223H2 Gerrman

admin
Site Admin
Posts: 60567
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Scripts working only in step mode.

Post by admin »

PeterH wrote:No doubt:

Code: Select all

}   ELSE (0 != $size) {
is wrong syntax.

But wrong syntax always leads to unexpected results. (I think there can be no expected results for wrong sysntax - then it wouldn't be wrong...)

So the best would be to stop the script and show some message in such a situation.
(I think ELSE must be followed by {...} or maybe by end-of-statement - so it can be recognized.)

I've seen some rather complex scripts here - and reporting recognizable scripting-errors to the script-"programmer" could help a lot on debugging, I think.
Agreed and done.

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: Scripts working only in step mode.

Post by tiago »

Yes, I was blindly sure that things should be like zer0 said ("ELSE must be the last block in the control structure."). So if ELSEIF must be stated, I have to end the structure with an ELSE { -do nothing- } like PeterH told, right? Under certain circumstances, IF/THEN/ELSE got me the same way ; )
But I saw no word about the XY crashing with the second script, which out of curiosity I tested on an old version and worked as expected. Just noticed the beta section and it contains info I could scroll through after a clue but if someone has an answer that would save me work which may end in nothing.
Power-hungry user!!!

admin
Site Admin
Posts: 60567
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Scripts working only in step mode.

Post by admin »

tiago wrote:Yes, I was blindly sure that things should be like zer0 said ("ELSE must be the last block in the control structure."). So if ELSEIF must be stated, I have to end the structure with an ELSE { -do nothing- } like PeterH told, right? Under certain circumstances, IF/THEN/ELSE got me the same way ; )
But I saw no word about the XY crashing with the second script, which out of curiosity I tested on an old version and worked as expected. Just noticed the beta section and it contains info I could scroll through after a clue but if someone has an answer that would save me work which may end in nothing.
OK, you are right. I can confirm an issue here in recent versions that has not been there before 20101220. Fix comes with next version.

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: Scripts working only in step mode.

Post by tiago »

Since zer0 said about the race conditions and looking after help with an Xyplorer experienced friend we tried few variations with "focus" "wait" and another one I forgot looking after a way to slow down things a bit and get the job done but no go.
Now I have a neat window telling the syntax is wrong, but considering the problem itself I think I'm lost. Ah, IF/THEN/ELSE... lol

I find it strange that a good bunch of commands support the parameter "i", but the obvious deserving it "pane" does not.

echo get("pane", i); // same with echo("<get pane i>");

reports the same as

echo get("pane"); // same with echo("<get pane>");

Playing a bit with "foreach" I got good results. But this one is giving me clues that perhaps it does not play nicely along with some commands, despite so far this is the only real situation I got.

end(compare("<xyver>", '9.90.0402', 'v') < 0, "Written for XYplorer v9.90.0402+.");
foreach($token, <get selecteditemsnames |>) {

selfilter """$token""", f; //using "sel" was not a solition!

focus "L"; //wasn't "focus" supposed to solve this? As I think, "selfilter" won't even require such a thing!

end (<curitem>=="", "Select one file to append its name to clipboard."); //debug code.

copytext("<curname><crlf>"), a;

}

echo "<clipboard>"; //it was supposed to echo a list of files, not only the first one appended in the then current clipboard content. This is just a test code to show the problem with "foreach vs selfilter".

Race condition, too? How? And most important: how to solve that?
Power-hungry user!!!

Post Reply