Author Topic: Create new folder with name of the selected files and move them to that folders?  (Read 32502 times)

Numian

  • Newbie
  • *
  • Posts: 3
    • View Profile
Hello,

is there a way to move selected files to newly created folder which will have the same names a seldcted files?

For example, I have a file 000.txt and 111.txt. I would like to create folder 000 and 111 and move those two txt files to appropriate folder.

Thank you.

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4455
    • View Profile
    • Multi Commander
It should be possible.

use GetSourceSelectPath(); to get list of all selected items
Loop all items and use PathGetNamePart(..);  to get the name part
the build the target path of using that hand use MoveFile(...) to move the file

see
http://multicommander.com/docs/multiscript/functions/getfilefromview
http://multicommander.com/docs/multiscript/functions/filesystem

Numian

  • Newbie
  • *
  • Posts: 3
    • View Profile
Ok, so i finally start to play with it. Unfortunatelly I didn'tget veryb far...

Code: [Select]
@var $files_path = GetSourceSelectedPaths();
@var $source_path = GetSourcePath();
LogDumpArray($files_path);
@var $n;
@var $name;
@var $name_array;
for( $n=0; $n<arrayCount($files_path); $n++ ) {
    $name = PathGetNamePart($files_path[$n], 1);
    arrayAdd($name_array, $name);
    LogDumpArray($name_array);    
}

and error log:
Code: [Select]
2018-02-02 13:00:59.782 ==[ Variable : $files_path ]==
2018-02-02 13:00:59.782 [0] : "C:\Users\BigMike\Desktop\Test\001.txt"
2018-02-02 13:00:59.783 [1] : "C:\Users\BigMike\Desktop\Test\002.txt"
2018-02-02 13:00:59.783 [2] : "C:\Users\BigMike\Desktop\Test\003.txt"
2018-02-02 13:00:59.783 [3] : "C:\Users\BigMike\Desktop\Test\004.txt"
2018-02-02 13:00:59.783 ===========================
2018-02-02 13:00:59.783 Script engine error => No matching function with name "arrayAdd" found.
2018-02-02 13:00:59.783 Script engine error - Line : 9, Error : -1 => Code : "arrayAdd($name_array, $name)"

What's going on? How can I create an array with selected file names then so I can use it in loop i.e. like this?
Code: [Select]
MakeDir($source_path ^ $name[$n], "NODIALOG,NEWQUEUE");
Thank you for any suggestions.
« Last Edit: February 02, 2018, 13:38:29 by Numian »

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4455
    • View Profile
    • Multi Commander
I think the error shown is because of other errors..

@var name_array; is not defined as a array..  you need [] after it for the system to know it is an array

@var $name_array[];

Also the script engine is a bit unsmart :),  the { } must be on its own line. (Since the engine is line based. It is not very smart.. )

Code: [Select]
@var $files_path = GetSourceSelectedPaths();
@var $source_path = GetSourcePath();
LogDumpArray($files_path);
@var $n;
@var $name;
@var $name_array[];
for( $n=0; $n < arrayCount($files_path); $n++ )
{
    $name = PathGetNamePart($files_path[$n], 1);
    arrayAdd($name_array, $name);
    LogDumpArray($name_array);    
}

Numian

  • Newbie
  • *
  • Posts: 3
    • View Profile
Thank you very much, Mathias! I seem like you know thing or two about this great piece of software... ;D

So here is my final script:
Code: [Select]
@var $files_path = GetSourceSelectedPaths();
@var $source_path = GetSourcePath();
@var $slash = "\\";
@var $n;
@var $name;
@var $name_array[];
for( $n=0; $n < arrayCount($files_path); $n++ )
{
  $name = PathGetNamePart($files_path[$n], 1);
  arrayAdd($name_array, $name);
  MakeDir($source_path ^ $name_array[$n], "NODIALOG,NEWQUEUE");
  MoveFile($source_path ^ $name_array[$n] + $slash, $files_path[$n], "NODIALOG,NEWQUEUE");
}

It does what I wanted, which is just awesome because I save a lot of time. But are there any improvements worth implement?

Thank you again!

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4455
    • View Profile
    • Multi Commander
I do not think you need the MakeDir command since Copy/move can create the target folder if it does not exists.
just make sure the target path ends with a backslash \