Select files with same name pattern as selected

Features wanted...
TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Select files with same name pattern as selected

Post by TheQwerty »

Changing focus back to the list can be achieved using

Code: Select all

Focus 'List';
Part of the problem here is that Don still hasn't made SelFilter work with regular expressions so it's painful to match 1 or more digits easily. (It's about time this changed, isn't it Don? :mrgreen:)

Which means the next easiest option is to Filter the list, which changes the focus, selection, scrolling, etc.

Give this script a try:

Code: Select all

"Select Numbered Base"
    //If no selection select the focused item first.
    if (Get('CountSelected') < 1) {
        Sel '+0';
    }

    //Sanity check that something is selected.
    End Get('CountSelected') < 1, 'No item selected...';

    $base = "<curbase>";
    $ext = "<curext>";

    //<cur...> only works on the focused and selected item.
    End "$base" Like '', 'Focused item is not selected...';

    //Strip numbers from the end
    $base = RegexReplace("$base", '^(.*?)(\d+)$', '$1');

    //Get list of files matching this base.
    $list = ListPane('a', "$base*", 5);

    //Rebuild the list of matches (ListPane may return items that matched more than we wanted)
    //Base = "test.part"
    //ListPane would include "test.part1.invalid.rar|test.part1.rar"
    $results = '';
    $pattern = "^$base\d+\.$ext$";
    foreach ($token, "$list") {
        $match = RegexReplace("$token", "$pattern", ':MATCH:');
        if ("$match" Like ':MATCH:') {
            $results = "$results|$token";
        }
    }
    
    //Clean up the list.
    $results = FormatList("$results", 'ted');

    //Select the items without changing focus.
    SelectItems "$results", 0, 0;
    
    //Refocus the list.
    Focus 'List';
It takes a rather roundabout way to achieve the same thing but avoids using filter, and thus it shouldn't change anything about the list other than the selection.

highend
Posts: 13315
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Select files with same name pattern as selected

Post by highend »

Ok, now I see what you mean.

just replace this

Code: Select all

  if ($f != ""){
     //filter for user pattern, (show matching files only):
       filter "*$f*";

     //select all (visible/filtered) files:
       sel "a";

     //disable filter to show all files again (selected and un-selected):
       filter;
with

Code: Select all

//filter for user pattern, (show matching files only):
       selfilter "$f", f, , 0;
Delete the sel "a"; and filter; lines completely.

Should solve this issue.

Complete script:

Code: Select all

$f = input( "Modify and leave wanted parts of file name only<crlf>
               to select all other files with same part too. <crlf>
               Tip: use empty filter to disable filter again", "<curname>",
               regexreplace("<curbase>", "(.+?)\d*", "$1"));

  if ($f != ""){
     //filter for user pattern, (show matching files only):
       selfilter "$f", f, , 0;

  }
  else{
      //deselect all:
        sel;
  }
One of my scripts helped you out? Please donate via Paypal

Reve_Etrange
Posts: 91
Joined: 19 Mar 2008 18:15

Re: Select files with same name pattern as selected

Post by Reve_Etrange »

Thanks TheQwerty and highend, both solutions seem to work.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Select files with same name pattern as selected

Post by TheQwerty »

highend wrote:Complete script:

Code: Select all

$f = input( "Modify and leave wanted parts of file name only<crlf>
               to select all other files with same part too. <crlf>
               Tip: use empty filter to disable filter again", "<curname>",
               regexreplace("<curbase>", "(.+?)\d*", "$1"));

  if ($f != ""){
     //filter for user pattern, (show matching files only):
       selfilter "$f", f, , 0;

  }
  else{
      //deselect all:
        sel;
  }
This may work for Reve_Etrange but it leaves a few edge cases untouched.

Code: Select all

regexreplace("<curbase>", "(.+?)\d*", "$1")
A base of "Testing123Alpha2" will cause this to return "TestingAlpha", so anchoring it to the end of the line would be better (returning "Testing123Alpha"):

Code: Select all

regexreplace("<curbase>", "(.+?)\d*$", "$1")
A second problem is if the pattern given to SelFilter does not contain a wildcard it automatically adds them so the above is the same as:

Code: Select all

SelFilter "*$f*", f, ,0;
Which means it would also select "PostTesting123Alpha". Manually adding the trailing wildcard eliminates this issue:

Code: Select all

SelFilter "$f*", f, , 0;
Unfortunately this still leaves a third problem in that it would now match "Testing123Alpha300OldVersion", and there really isn't a good way to avoid this using SelFilter.


Of course, if these types of overlaps do not occur often then there really isn't any reason to worry about them, but it helps to be aware of them nonetheless.

Reve_Etrange
Posts: 91
Joined: 19 Mar 2008 18:15

Re: Select files with same name pattern as selected

Post by Reve_Etrange »

Ah, I wasn't sure what you meant about selfilter, got it now.

Biggynuff
Posts: 108
Joined: 13 May 2010 14:08

Re: Select files with same name pattern as selected

Post by Biggynuff »

I'm very grateful for this script 8)

I had over 30 files named:

Code: Select all


Don_surprised_by_2_blondes_while_updating_XY.part01.rar

and a few such as:

Code: Select all


I_wish_I_had_a_maid.some.chance

Now all we need is a new scripting command to make our dreams come true! :D

Perhaps a User Button that opens the CD writer tray and a life size model of whoever you're thinking about pops out. WOW... sales of XY would go through the roof :shock: I'm working on it now :P

eil
Posts: 1621
Joined: 13 Jan 2011 19:44

Re: Select files with same name pattern as selected

Post by eil »

it seems not only me noticed "those names" of archive parts :D
Win 7 SP1 x64 100% 1366x768

Reve_Etrange
Posts: 91
Joined: 19 Mar 2008 18:15

Re: Select files with same name pattern as selected

Post by Reve_Etrange »

It's called XY for a reason--this is a file manager for men, obviously.

The API for chicks wants some improvement, by the way.
Eg.

Code: Select all

::makecoffee;    [X]
::makesandwich;  [ ]
::cleansocks;    [ ]
::gimmebreak;    [ ]

Biggynuff
Posts: 108
Joined: 13 May 2010 14:08

Re: Select files with same name pattern as selected

Post by Biggynuff »

Ha Ha HA :lol: :lol: :lol:

That's fantastic!

Have to be honest Reve_Etrange, I was falling about laughing when I read those file names :D I would have chickened out and used something like 'abc.part01.rar'

Your honesty is a breath of fresh air. Nice one 8)

Reve_Etrange
Posts: 91
Joined: 19 Mar 2008 18:15

Re: Select files with same name pattern as selected

Post by Reve_Etrange »

I'm trying to have the script open a dialog in the end listing the selected files and deleting them if the user press "ok"'.
How would you do that?

highend
Posts: 13315
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Select files with same name pattern as selected

Post by highend »

if statement + confirm() command.
One of my scripts helped you out? Please donate via Paypal

Reve_Etrange
Posts: 91
Joined: 19 Mar 2008 18:15

Re: Select files with same name pattern as selected

Post by Reve_Etrange »

A final:

Code: Select all

Delete 1, 1
seems to do the job, unless I am missing something.

Reve_Etrange
Posts: 91
Joined: 19 Mar 2008 18:15

Re: Select files with same name pattern as selected

Post by Reve_Etrange »

Minor change to TheQwerty's script:

Code: Select all

    $results = '';
    $base = replacelist($base, "(,)", "\(,\)", ",");
    $pattern = "^$base\d+\.$ext$";
You want to escape the $base, since it is used as a regexp pattern later on and special characters will have special meaning (and could make the match fail).
Obviously there is more to it than just parens, but you get the idea.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Select files with same name pattern as selected

Post by TheQwerty »

Reve_Etrange wrote:Minor change to TheQwerty's script:

Code: Select all

    $results = '';
    $base = replacelist($base, "(,)", "\(,\)", ",");
    $pattern = "^$base\d+\.$ext$";
You want to escape the $base, since it is used as a regexp pattern later on and special characters will have special meaning (and could make the match fail).
Obviously there is more to it than just parens, but you get the idea.
Opps.. :oops: Thanks for pointing that out!

Indeed it should be done to both the base and extension here. Based on this the following should take care of escaping the majority of special characters:

Code: Select all

$results = '';
$regexEscape = '([\[\\\^\$\.\|\?\*\+\(\)\{\}])';
$base = RegexReplace("$base", "$regexEscape", '\$1');
$ext = RegexReplace("$ext",  "$regexEscape", '\$1');
$pattern = "^$base\d+\.$ext$";

Reve_Etrange
Posts: 91
Joined: 19 Mar 2008 18:15

Re: Select files with same name pattern as selected

Post by Reve_Etrange »

Thanks for the more comprehensive escaping :)

Post Reply