Page 1 of 1

[Solved] How do I synchronously execute commands?

Posted: 16 Jul 2011 16:03
by Sander Bouwhuis
I want to create a user command using 'Open' or 'Open with' to optimize PNG images using PngOut.

So, I have the 'PngOut.exe' executable, but it only accepts 1 file name per invocation. When I use the 'Open with' command with the 'Multiple instance' setting and the '<items>' parameter, XyPlorer starts all the commands at the same time. So, selecting 100 files means that XyPlorer will start PngOut 100 times at the same time!
This is obviously enough for a hard reset of my computer, because Windows totally dies on me.

How do I start 1 after another (synchronously) instead of all at once (asynchronously).
Any help would be highly appreciated.

Re: How do I synchronously execute commands?

Posted: 16 Jul 2011 16:20
by highend
I don't think there is a build in solution for this kind of task (if you work with multiple selected files).

The normal way (for me) is to use writeline() to build a batch file and execute it at the end of the (xyplorer) script with open().

Re: How do I synchronously execute commands?

Posted: 16 Jul 2011 16:39
by Stefan
iycgtptyarvg wrote:When I use the 'Open with' command with the 'Multiple instance' setting and the '<items>' parameter, XyPlorer starts all the commands at the same time. So, selecting 100 files means that XyPlorer will start PngOut 100 times at the same time!
Seams to me the openwith() as command and also in UDC missed an Wait=1 parameter.

Maybe this could be an improvement wish to Donald:

openwith application, [mode (s|m|W)], [item]
where 'w' is the same as 'm' but will wait till the called process has finished the task right before.



In the meantime....
How do I start 1 after another (synchronously) instead of all at once (asynchronously).
You can use an UDC with "Run Script"
and use an script like this example:

Code: Select all

$SelFiles = get("SelectedItemsPathNames", "|");

   foreach($file, $SelFiles){
         
          $file = quote($file);
            
           msg "run myExe with $file as parameter",1;
          ///// run command, [directory], [wait=0  (0=return immediately, 1=wait till the process has finished)]
          //  run notepad $file,,1;
            
   }
Only problem with such openwith and run can be the right quoting. Read XY help or see >here< for an little help.

HTH?

Re: How do I synchronously execute commands?

Posted: 16 Jul 2011 17:09
by Sander Bouwhuis
Ok, thanks for the help!

The final script turned out as such:

Code: Select all

$SelFiles = get("SelectedItemsPathNames", "|");

  foreach($file, $SelFiles)
  {
     $file = quote($file);
     run "E:\Utilities\XnView\AddOn\PngOut.exe" $file, , 1
  }
There are only 2 problems left:

1.
XyPlorer's GUI didn't react anymore while the script was running.

How can I solve this?

2.
How can I prevent the shells / command prompts / dos boxes from popping up? I.e., I want the shells to start minimized so I don't lose the focus 100 times.

Re: How do I synchronously execute commands?

Posted: 16 Jul 2011 17:47
by Stefan
iycgtptyarvg wrote: 1.
XyPlorer's GUI didn't react anymore while the script was running.

How can I solve this?
Then better go the highend way and out-source the work to an DOS batch:

Code: Select all

////// Wrap selected file names into an DOS command each, write that to an temp CMD file and execute it
////// Modify the 'user settings' to your own needs, select your files and execute this script


    ////// User settings:
    $shell = "start /wait /min";  // read syntax in a DOS-Box with command> start /?
    $app =  quote("X:\path to\app.exe");
    $paraBefore = "";
    $Files = get("SelectedItemsPathNames", "|");
    $paraAfter = "";


    ////// For Each File In FileS Do: add an line to an array, build from all parameters
                   ////// initialize the $array var, and write the header of an batch file:
                   $array="@ECHO OFF<crlf>";
                
                   foreach($file, $Files){       
                        $file = quote($file);        
                        $array = $array . "$shell $app $paraBefore $file $paraAfter<crlf>";        
                   }
    
    
    ////// Test output:
    //text $array;


    ////// Write this to an temporary batch file:
    $tempfile = "%temp%\xybatch<date yymmdd>.cmd";
    writefile($tempfile, $array); 
     
    ////// control it:
    text readfile($tempfile);
    
    ////// prompt the user to execute or cancel:
    msg "Continue? Really execute the batch?",1;
    ///if [OK] clicked, do it:
    run quote($tempfile);



- - -


iycgtptyarvg wrote: run "E:\Utilities\XnView\AddOn\PngOut.exe" $file, , 1

2.
How can I prevent the shells / command prompts / dos boxes from popping up?
I.e., I want the shells to start minimized so I don't lose the focus 100 times.
try
run cmd /c "E:\Utilities\XnView\AddOn\PngOut.exe" $file, , 1

- - -
EDIT:
because of <post below> and putting the in quotes for the run() command.

EDIT2:
'Than' should be 'Then'

Re: How do I synchronously execute commands?

Posted: 16 Jul 2011 18:25
by highend
Seams to me the openwith() as command and also in UDC missed an Wait=1 parameter.

Maybe this could be an improvement wish to Donald:

openwith application, [mode (s|m|W)], [item]
where 'w' is the same as 'm' but will wait till the called process has finished the task right before.
In the way that a foreach loop could start the first openwith, wait till it's finished, enter the second loop, start the second openwith etc.? And that all in a background mode where XYplorer is usable while the script is running? At least that would be a long anticipated feature :)

Re: How do I synchronously execute commands?

Posted: 16 Jul 2011 19:21
by Sander Bouwhuis
Wow Stefan, you are awesome!

I ended up with this:

Code: Select all

  $array="";
  $Files = get("SelectedItemsPathNames", "|");

  foreach($file, $Files)
  {
    $file = quote($file);
    $array = $array . "start ""PngOut"" /wait /min /low ""E:\Utilities\XnView\AddOn\PngOut.exe"" $file<crlf>";
  }

  $tempfile = "E:\Temp\PngOutBatch_<date yyyy-mm-dd_hh-nn-ss>.bat";
  writefile($tempfile, $array);
  run "$tempfile";
For some strange reason, I cannot use the run "$tempfile"; command when there are spaces in the $tempfile name; that is why I used underscores. Is there anything I did wrong?

Re: How do I synchronously execute commands?

Posted: 16 Jul 2011 20:14
by Stefan
iycgtptyarvg wrote: For some strange reason, I cannot use the run "$tempfile"; command when there are spaces in the $tempfile name; that is why I used underscores. Is there anything I did wrong?
As always, if there are spaces in path or file name, you have to put the whole string into quotes.

-

I wanted to do this too, but i had problems, i always get an error message:

"Die Syntax für den Dateinamen, Verzeichnisnamen oder die Datenträgerbezeichnung ist falsch."

Because i used this quoted path for writefile() and readfile() which both not understood to handle this (as far as i understand)

Wrong code:

Code: Select all

     $array ="@ECHO OFF & echo hello iycgtptyarvg &PAUSE";
    $tempfile = quote("%temp%\xybatch<date yymmdd>.cmd)";
    writefile($tempfile, $array);
    text readfile($tempfile);
     run $tempfile;

But the run() command launches the shell execute which needs quotes around path with spaces.
So the trick is to quote the path only for the run() but not for the writefile() and readfile() command:

Working code:

Code: Select all

 $array ="@ECHO OFF & echo hello iycgtptyarvg &PAUSE";
    $tempfile = "%temp%\xybatch <date yymmdd>.cmd"; 
    writefile($tempfile, $array);
    text readfile($tempfile);
    run quote($tempfile);

Re: How do I synchronously execute commands?

Posted: 16 Jul 2011 22:46
by Sander Bouwhuis
Hmmm... this scripting stuff is quite confusing.

I can't thank you enough for this. Because of you, I'm now able to use this script for similar situations.

Thank you, thank you, thank you!