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

pncdaspropagandas

  • Contributor
  • Active Member
  • *****
  • Posts: 93
    • 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
  • VIP Member
  • *****
  • Posts: 4455
    • 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: 93
    • View Profile
Re: Create folder and move selected files
« Reply #2 on: September 21, 2017, 16:51:34 »
Thanks Mathias. Now it is working!  :)

mlabrkic

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Create folder and move selected files
« Reply #3 on: February 19, 2022, 18:58:25 »
tested and documented:
 :)

Code: [Select]
// Create_folder_and_move_selected_files, F12

// http://forum.multicommander.com/forum/index.php/topic,2079.0.html
// Create folder and move selected files to that folder
// Contributor: pncdaspropagandas

// #########################################################

// ### Docs ###
// Inserted, 2022-02M-20 16:20:25

// User Defined Commands - MultiScript:
// http://multicommander.com/docs/UDC_multiscript

// MultiScript Language Syntax:
// http://multicommander.com/docs/multiscript/languagesyntax

// ---------------------------------------------------------
// http://multicommander.com/docs/multiscript/functions/misc

// AskText
// <num> AskText( <str> label, <str> inputText, <num> option );
// http://multicommander.com/docs/multiscript/functions/misc#asktext

// MessageBox
// <num> MessageBox( <str> caption, <str> text, <num> options );
// http://multicommander.com/docs/multiscript/functions/misc#messagebox

// Log - Log text to a log window.
// <num> Log( <num> logID, <num> logLevel, <str> logText );
// http://multicommander.com/docs/multiscript/functions/misc#log

// ---------------------------------------------------------
// http://multicommander.com/docs/multiscript/functions/filesystem

// FileExists
// @var $res = FileExists( "C:\\temp\\MyFile.txt" );
// http://multicommander.com/docs/multiscript/functions/filesystem#fileexists

// IsFolder
// @var $res = IsFolder( "C:\\temp" );
// http://multicommander.com/docs/multiscript/functions/filesystem#isfolder

// MakeDir
// MakeDir( "C:\\Temp\NewFolder\\SubFolder\\", "LOCAL,RECURSIVE" );
// MakeDir( "C:\\Temp\\MyFiles.zip\\Folder", "" );
// http://multicommander.com/docs/multiscript/functions/filesystem#makedir

// MoveFile
// MoveFile( $targetPath, $sourceFile, "NODIALOG, OVERWRITE_ALL" );
// http://multicommander.com/docs/multiscript/functions/filesystem#movefile

// ---------------------------------------------------------
// http://multicommander.com/docs/multiscript/functions/string

// StrSub
// <str> StrSub(<str> input, <num> offset, <num> length);
// http://multicommander.com/docs/multiscript/functions/string#strsub

// StrIsEqual
// <num> StrIsEqual(<str> string1, <str> string2);
// http://multicommander.com/docs/multiscript/functions/string#strisequal

// StrLen
// @var $len = StrLen( $str );
// http://multicommander.com/docs/multiscript/functions/string#StrLen

// ---------------------------------------------------------
// MultiScript - Get and Set Selection and Paths
// http://multicommander.com/docs/multiscript/functions/getfilefromview

// GetSourceSelectedPaths
// @var $arr = GetSourceSelectedPaths("IGNORE_FOCUS");
// Get an array with the full paths of all selected items in the source view.
// http://multicommander.com/docs/multiscript/functions/getfilefromview#getsourceselectedpaths

// ---------------------------------------------------------
// MultiScript - Array functions
// http://multicommander.com/docs/multiscript/functions/array


// #########################################################

// 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,NOWAIT,USEEXISTINGQUEUE");
}
else
{
MoveFile($source_path ^ $answer + $slash, $selected_files[$n], "NODIALOG,NOWAIT,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;
}
}

« Last Edit: February 22, 2022, 08:09:47 by mlabrkic »

mlabrkic

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Create folder and move selected files
« Reply #4 on: February 28, 2022, 15:43:17 »
MultiCommander script:
Create folder (your input + current date and time)
and move selected files to that folder (+ docs/multiscript/functions)

Code: [Select]
// Create_folder_and_move_selected_files_date, F12

// MultiCommander script:
// Create folder (your input + current date and time)
// and move selected files to that folder (+ docs/multiscript/functions)

// http://forum.multicommander.com/forum/index.php/topic,2079.0.html
// Contributor: pncdaspropagandas

// #########################################################

// ### Docs ###
// Inserted, 2022-02M-20 16:20:25

// User Defined Commands - MultiScript:
// http://multicommander.com/docs/UDC_multiscript

// MultiScript Language Syntax:
// http://multicommander.com/docs/multiscript/languagesyntax

// ---------------------------------------------------------
// http://multicommander.com/docs/multiscript/functions/misc

// AskText
// <num> AskText( <str> label, <str> inputText, <num> option );
// http://multicommander.com/docs/multiscript/functions/misc#asktext

// MessageBox
// <num> MessageBox( <str> caption, <str> text, <num> options );
// http://multicommander.com/docs/multiscript/functions/misc#messagebox

// Log - Log text to a log window.
// <num> Log( <num> logID, <num> logLevel, <str> logText );
// http://multicommander.com/docs/multiscript/functions/misc#log

// ---------------------------------------------------------
// http://multicommander.com/docs/multiscript/functions/filesystem

// FileExists
// @var $res = FileExists( "C:\\temp\\MyFile.txt" );
// http://multicommander.com/docs/multiscript/functions/filesystem#fileexists

// IsFolder
// @var $res = IsFolder( "C:\\temp" );
// http://multicommander.com/docs/multiscript/functions/filesystem#isfolder

// MakeDir
// MakeDir( "C:\\Temp\NewFolder\\SubFolder\\", "LOCAL,RECURSIVE" );
// MakeDir( "C:\\Temp\\MyFiles.zip\\Folder", "" );
// http://multicommander.com/docs/multiscript/functions/filesystem#makedir

// MoveFile
// MoveFile( $targetPath, $sourceFile, "NODIALOG, OVERWRITE_ALL" );
// http://multicommander.com/docs/multiscript/functions/filesystem#movefile

// ---------------------------------------------------------
// http://multicommander.com/docs/multiscript/functions/string

// StrSub
// <str> StrSub(<str> input, <num> offset, <num> length);
// http://multicommander.com/docs/multiscript/functions/string#strsub

// StrIsEqual
// <num> StrIsEqual(<str> string1, <str> string2);
// http://multicommander.com/docs/multiscript/functions/string#strisequal

// StrLen
// @var $len = StrLen( $str );
// http://multicommander.com/docs/multiscript/functions/string#StrLen

// ---------------------------------------------------------
// MultiScript - Get and Set Selection and Paths
// http://multicommander.com/docs/multiscript/functions/getfilefromview

// GetSourceSelectedPaths
// @var $arr = GetSourceSelectedPaths("IGNORE_FOCUS");
// Get an array with the full paths of all selected items in the source view.
// http://multicommander.com/docs/multiscript/functions/getfilefromview#getsourceselectedpaths

// ---------------------------------------------------------
// MultiScript - Array functions
// http://multicommander.com/docs/multiscript/functions/array


// #########################################################

@var $now = GetTime();
@var $date = FormatDate( "yyyy-MMm-dd" , $now );
@var $time = FormatTime( "HH-mm" , $now );

// 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 = $source_path ^ ($answer + "_" + $date + "_" + $time);
$answer = $answer + "_" + $date + "_" + $time;

// ---------------------------------------------------------

// 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,NOWAIT,USEEXISTINGQUEUE");
}
else
{
MoveFile($source_path ^ $answer + $slash, $selected_files[$n], "NODIALOG,NOWAIT,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;
}
}

« Last Edit: February 28, 2022, 15:48:01 by mlabrkic »