Author Topic: Create folder and move selected files  (Read 259 times)

pncdaspropagandas

  • Contributor
  • Active Member
  • *****
  • Posts: 87
    • View Profile
Create folder and move selected files
« on: September 18, 2017, 18:43:23 »
Hi, I tried to write a script to do 2 operations at once:
- Create a subfolder
- Move the selected files to that subfolder

So I wrote:
Code: [Select]
// Ask user for folder name
@var $answer = AskText("Type name of the new folder to create and move selected files.", "New folder", 0);

// If user canceled, abort
if ( $answer == 0 )
{
break;
}

// Folder exists?
@var $source_path = GetSourcePath();
if( FileExists($source_path ^ $answer) > 0 )
{
MessageBox("Error", "Typed folder already exists.", 0);
break;
}
else
{
// Create empty folder
// Check if source path is LOCAL
@var $options;
if( IsFolder($source_path) )
{
@var $colon_check = StrSub($source_path, 1, 1);
if( StrIsEqual($colon_check, ":") )
{
$options = "LOCAL,RECURSIVE";
}
else
{
$options = "";
}
}
else
{
$options = "";
}

if( MakeDir($source_path ^ $answer, $options) == 0 )
{
Log( 1, 10, "Created : " + $source_path ^$answer );
@var $selected_files = GetSourceSelectedPaths();
@var $n;
@var $len = StrLen($answer) - 1;
@var $slash = StrSub($answer, $len, 1);
if( StrIsEqual($slash, "\\") )
{
$slash = "";
}
else
{
$slash = "\\";
}
for( $n=0; $n<arrayCount($selected_files); $n++ )
{
// MoveFile has a bug where target path has to end with slash
if( $n == 0 )
{
MoveFile($source_path ^ $answer + $slash, $selected_files[$n], "NODIALOG,NEWQUEUE");
}
else
{
MoveFile($source_path ^ $answer + $slash, $selected_files[$n], "NODIALOG,USEEXISTINGQUEUE");
}
}

// Select created folder
// SetClipboardText($source_path ^ $answer);
// MC.RunCmd ID="Core.1312"
// MC.Explorer.Selection.UnselectAll
}
else
{
MessageBox("Error", "Error while creating folder.", 0);
break;
}
}

but when I have a lot of files selected, MC hangs while executing the MoveFile()..... I don't know if I did something wrong. Do I have indeed to move each file individualy inside a for?

Mathias (Author)

  • Administrator
  • Elite Member
  • *****
  • Posts: 3044
    • View Profile
    • Multi Commander
Re: Create folder and move selected files
« Reply #1 on: September 21, 2017, 09:45:05 »
Problem is that script are normally not run in the background.. The run in the same context as the UI (because you can interact with the UI from script)
So when running a script that takes times.. the main UI will hang until the script is done.

You can tell MoveFile to not wait until it is done by adding "NOWAIT",  Then it will start all of the right after each other without waiting.
So best to use  USEEXISTINGQUEUE


pncdaspropagandas

  • Contributor
  • Active Member
  • *****
  • Posts: 87
    • View Profile
Re: Create folder and move selected files
« Reply #2 on: September 21, 2017, 16:51:34 »
Thanks Mathias. Now it is working!  :)