Page 1 of 1

rename files including wildcards

Posted: 15 Jan 2013 01:07
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

Re: rename files including wildcards

Posted: 15 Jan 2013 09:27
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

Re: rename files including wildcards

Posted: 15 Jan 2013 10:00
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.

Re: rename files including wildcards

Posted: 15 Jan 2013 10:14
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?

Re: rename files including wildcards

Posted: 15 Jan 2013 10:27
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.

Re: rename files including wildcards

Posted: 15 Jan 2013 10:32
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.

Re: rename files including wildcards

Posted: 15 Jan 2013 10:37
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.

Re: rename files including wildcards

Posted: 15 Jan 2013 10:44
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, ...

.

Re: rename files including wildcards

Posted: 15 Jan 2013 10:46
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...

Re: rename files including wildcards

Posted: 15 Jan 2013 12:02
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")

Re: rename files including wildcards

Posted: 15 Jan 2013 12:13
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");