rename files including wildcards

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
mewallis7
Posts: 1
Joined: 15 Jan 2013 00:53

rename files including wildcards

Post by mewallis7 »

I've read through the help file and searched the forums, but I cannot find a solution to what should be a simple problem. How do I do batch file renames using search and replace with wildcards. Example:

old name * S??E?? *.*
new name * - s??e?? - *.*

I'd like to write a script that can be attached to a button.

I used to do this sort of thing with PowerDesk and I think even XTree, but XYplorer only partially supports it.

TIA, Mike Wallis

admin
Site Admin
Posts: 66375
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: rename files including wildcards

Post by admin »

Hi and welcome,

You can very probably do this using a Regular Expression (RegExp Rename). But I cannot help you with the exact pattern you need. I'm sure some of the RegExp experts here will soon tell you more...

Don

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

Re: rename files including wildcards

Post by highend »

If Don could provide a regexsearch() command, which only stores found patterns in variables, it would be a simple script...

But you aren't able to convert to lowercase via the integrated regex implementation (if I'm not missing something obvious) and I'm currently too lazy to think about a "super smart" way to handle this with regexreplace() because you would have to use the same pattern twice (to separate the inner match from the first and last).

Without converting to lowercase and assuming that your ?? are word characters (letters, digits) e.g.:

Code: Select all

$a = regexreplace("aaaSbbEccxxx.txt", "(.*?)(S\w{2}E\w{2})(.*)", "$1 - $2 - $3");
    text $a;
Even with regexsearch() it's complicated...
recase() converts strings to upper, lower, etc. but you have to use multiple reference groups to convert only single characters. If your pattern is really strict: yes, can be done.
Last edited by highend on 15 Jan 2013 10:15, edited 1 time in total.
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 66375
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: rename files including wildcards

Post by admin »

highend wrote:If Don could provide a regexsearch() command, which only stores found patterns in variables, it would be a simple script...
I'm really regexp dumb. Could you elaborate a little on this. How should this command work?

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

Re: rename files including wildcards

Post by highend »

I'm really regexp dumb. Could you elaborate a little on this. How should this command work?
E.g.:
regexsearch(string, pattern, variables, [matchcase])

It should search for the pattern and create variables for each capture group.

Code: Select all

$match = regexsearch("aaaSbbEccxxx.txt", "(.*?)(S\w{2}E\w{2})(.*)", $cg1, $cg2, $cg3);
 text $cg1." - ".recase($cg2, "lower")." - ".$cg3;
Would output:
aaa - sbbecc - xxx.txt
Ofc the regex pattern should be modified to match the op's request (only the S and the E would be converted, not the whole string).

Afaik there was already a suggestion to implement a new regex command that doesn't replace at the same moment.
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 66375
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: rename files including wildcards

Post by admin »

highend wrote:Afaik there was already a suggestion to implement a new regex command that doesn't replace at the same moment.
Yes, from Marco I think.

The problem is: What you suggest needs function arguments that return values. This is not supported currently and not easy to do. That would be a major task for me.

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: rename files including wildcards

Post by Stefan »

mewallis7 wrote:How do I do batch file renames using search and replace with wildcards. Example:

old name * S??E?? *.*
new name * - s??e?? - *.*
Hi Mike, welcome.

You could do that by using an XYplorer script:

Code: Select all

$oldname = "File Name part S12E34 Trailing Part.ext";
//new name * - s??e?? - *.*

 //Get the various parts of the file name:
 $P1 = regexreplace($oldname,"(.+ )(S\d\d)(E\d\d)( .+)", "$1");
 $P2 = regexreplace($oldname,"(.+ )(S\d\d)(E\d\d)( .+)", "$2");
 $P3 = regexreplace($oldname,"(.+ )(S\d\d)(E\d\d)( .+)", "$3");
 $P4 = regexreplace($oldname,"(.+ )(S\d\d)(E\d\d)( .+)", "$4");


 //if our regexpattern match:
 if($P1 != $oldname){

    //recase part 2 + 3:
    $P2 = recase($P2, "lower");
    $P3 = recase($P3, "lower");

    //do the renaming:
    msg "$oldname<crlf 2>$P1$P2$P3$P4";
 }
Result:

Code: Select all

      ---------------------------
                 XYplorer
      ---------------------------
File Name part S12E34 Trailing Part.ext

File Name part s12e34 Trailing Part.ext
      ---------------------------
                 OK   
      ---------------------------

For how to use an script or add an button see my signature.

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: rename files including wildcards

Post by Stefan »

BTW, over at ReNamer we have the following script command "SubMatchesRegEx",
maybe this is from interest here too?

Code: Select all

var
  Parts: TStringsArray;
  
begin
  Parts := SubMatchesRegEx(WideExtractBaseName(FileName), '(.)(.+_)(.+_)(.+)', FALSE);
  If (Length(Parts) <=0) then exit;

 FileName := WideUpperCase(Parts[0]) + Parts[1]  + WideUpperCase(Parts[2]) + Parts[3]  + WideExtractFileExt(FileName);
end.

Basically that's:

$PARTS = SubMatchesRegEx( $input, "regex pattern" );

Returns an array of matching groups: $PARTS_1, $PARTS_2, ...

.
Last edited by Stefan on 15 Jan 2013 10:47, edited 1 time in total.

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

Re: rename files including wildcards

Post by highend »

This is not supported currently and not easy to do. That would be a major task for me.
Sure. It's just a suggestion. Would make things much easier for the user, not for you ;)

<curname> = aaaSBbECcxxx.txt

Code: Select all

$curName = "<curname>";
    $cg1 = regexreplace("$curName", "(.*?)(S)(\w{2})(E)(\w{2})(.*)", "$1");
    $cg2 = regexreplace("$curName", "(.*?)(S)(\w{2})(E)(\w{2})(.*)", "$2");
    $cg3 = regexreplace("$curName", "(.*?)(S)(\w{2})(E)(\w{2})(.*)", "$3");
    $cg4 = regexreplace("$curName", "(.*?)(S)(\w{2})(E)(\w{2})(.*)", "$4");
    $cg5 = regexreplace("$curName", "(.*?)(S)(\w{2})(E)(\w{2})(.*)", "$5");
    $cg6 = regexreplace("$curName", "(.*?)(S)(\w{2})(E)(\w{2})(.*)", "$6");
    text $cg1." - ".recase($cg2, "lower").$cg3.recase($cg4, "lower").$cg5." - ".$cg6;
This would be one possible solution but it's ähm, ugly as hell... One minor change and you need to edit 6 or more lines...
One of my scripts helped you out? Please donate via Paypal

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: rename files including wildcards

Post by Marco »

Simple one liner, works here

Code: Select all

regexreplace ("File Name part S12E34 Trailing Part.ext","S(\d)(\d)E(\d)(\d)","s$1$2e$3$4")
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: rename files including wildcards

Post by Stefan »

Marco wrote:Simple one liner, works here

Code: Select all

regexreplace ("File Name part S12E34 Trailing Part.ext","S(\d)(\d)E(\d)(\d)","s$1$2e$3$4")
:appl: good idea for that special renaming case :biggrin:

You can even shorten it:

Code: Select all

regexreplace ("File Name part S12E34 Trailing Part.ext","S(\d\d)E(\d\d)","s$1e$2");

Post Reply