Author Topic: Select Same Name (Selected Files)  (Read 25580 times)

pncdaspropagandas

  • Contributor
  • Active Member
  • *****
  • Posts: 93
    • View Profile
Select Same Name (Selected Files)
« on: February 02, 2017, 10:44:54 »
I wrote a Multi Script to extend the Select Same Name to All Selected Files:

@var $selected_files[] = GetSourceSelectedPaths();
@var $same_name_files[];
@var $same_name_files_iteration[];

@var $n;
@var $i;
@var $aux;

for ($n = 0; $n < arrayCount($selected_files); $n++)
{
    MC.Explorer.SetItemFocus ITEM={PathGetNamePart($selected_files[$n])}
    MC.RunCmd ID="ExplorerPanel.40308"
   
    // SetItemFocus causes the selected files in panel to be unselected
    // only the focused file gets selected
    // so we have to add it to a array and re-select them at the end
    $same_name_files_iteration = GetSourceSelectedFileNames();
    for ($i = 0; $i < arrayCount($same_name_files_iteration); $i++)
    {
        $aux = $same_name_files_iteration[$i];
        arrayAdd($same_name_files, $aux);
    }
}

SetSourceSelected($same_name_files, 0);

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4402
    • View Profile
    • Multi Commander
Re: Select Same Name (Selected Files)
« Reply #1 on: February 02, 2017, 11:00:59 »
It might be because you are mixing custom command and multiscript command in a strange way

MultiScript can call custom command, But it depends on what you do, Also the UI can't be updated until the script is finished since the script is running in the same thread.

Also MultiScript / Custom Command are different
MultiScript is accessing commands in MC at a low level. Behind the UI.  MultiScript does not need UI. ( Unless you call stuff that query UI stuff)
CustomCommand access functions from the UI side. But with the option of customizing the paramters.  But they are accessing it from the UI side, like if the user press the button for the action.

And note sure what you are trying to do.. But there is already commands for selecting all

pncdaspropagandas

  • Contributor
  • Active Member
  • *****
  • Posts: 93
    • View Profile
Re: Select Same Name (Selected Files)
« Reply #2 on: February 02, 2017, 12:51:53 »
Hmm ok

I did this script because when I select multiple files and run Edit > Select Same Name, I get only the focused file to select files with the same name. So I did this script that applies the internal function in a loop to all selected files.

In the image bellow, I selected both txt files and ran Select Same Name. The result is only 3 files selected intead of 4.

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4402
    • View Profile
    • Multi Commander
Re: Select Same Name (Selected Files)
« Reply #3 on: February 02, 2017, 21:28:35 »
Ok

There are two (possible more) way to do this with script.

#1
Almost as you already have done.. but
Do the matching you self in script.

Get list of all items in view
Get list of selected.
Loop selected list and for every item you loop the AllItemsInView list and if match found input into list.
when everything is done.. set the list with SetSourceSelected
and you can use StrIsWildMatch or StrIsWildMatchNoCase to do the matching.

#2
Get All Selected and loop  them.. and build string that will be a filter matching string. ( eg "Filename* Filename2* filename3*" )
then call custom command
MC.Explorer.Selection.Select  FILTER={$myFilter}

the filter string can look like "MyFile* MySecondFile* YouFile*" and so one..   So just get name and add a * and space after each
(Might need to quote them the filename can have spaces)

pncdaspropagandas

  • Contributor
  • Active Member
  • *****
  • Posts: 93
    • View Profile
Re: Select Same Name (Selected Files)
« Reply #4 on: February 03, 2017, 11:17:08 »
Did't think about that. That is way better indeed.

Thanks!

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4402
    • View Profile
    • Multi Commander
Re: Select Same Name (Selected Files)
« Reply #5 on: February 03, 2017, 11:44:57 »
Ahh and if you do #2, then you might want to keep the "." in the filter so that it only matching files with different extension.
"filename.* filename2.* filename3.*"



pncdaspropagandas

  • Contributor
  • Active Member
  • *****
  • Posts: 93
    • View Profile
Re: Select Same Name (Selected Files)
« Reply #6 on: February 06, 2017, 11:21:43 »
Thanks Mathias. I chose #1 and wrote the code:

@var $selected_files[] = GetSourceSelectedFileNames();
@var $all_files[] = GetSourceItems();
@var $same_name_files[];

@var $n;
@var $i;
@var $aux;
@var $aux2;
@var $selected_file_name_no_ext;

for($n = 0; $n < arrayCount($selected_files); $n++)
{
    $selected_file_name_no_ext = PathGetNamePart($selected_files[$n], 1);
    //LogAppInfo($selected_file_name_no_ext);
    for($i = 0; $i < arrayCount($all_files); $i++)
    {
        $aux = $all_files[$i];
        $aux2 = PathGetNamePart($aux, 0);
        //LogAppInfo('is ' + $aux2 + ' equal to ' + $selected_file_name_no_ext + ' ?');
      // For some reason if we compare using == instead of StrIsWildMatchNoCase, it always enters the if
        if (StrIsWildMatchNoCase($aux2, $selected_file_name_no_ext + '.*') == 1)
        {
            //LogAppInfo('equals');
            arrayAdd($same_name_files, $aux2);
        }
    }
   
}

 ;)
SetSourceSelected($same_name_files, 1);

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4402
    • View Profile
    • Multi Commander
Re: Select Same Name (Selected Files)
« Reply #7 on: February 06, 2017, 12:42:59 »
comparing with == can be tricky.. 
http://multicommander.com/docs/multiscript/functions/string#operator__
It also works like string compare in C/C++ and others. so a exact match is 0,

Also if both of the variables are not of string types, then automatic conversion comes into play that can create havoc.
So because of that it is better to use StrIsEqual / StrIsEqualNoCase  if you doing plain string compare

pncdaspropagandas

  • Contributor
  • Active Member
  • *****
  • Posts: 93
    • View Profile
Re: Select Same Name (Selected Files)
« Reply #8 on: February 06, 2017, 16:31:45 »
wow, dind't see that... I thought == would work just like StrIsEqual. That's why I was having a headache with the code lol.

Thanks Mathias