rename files including wildcards
-
mewallis7
- Posts: 1
- Joined: 15 Jan 2013 00:53
rename files including wildcards
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
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
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
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
FAQ | XY News RSS | XY X
-
highend
- Posts: 14956
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: rename files including wildcards
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.:
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.
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;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
I'm really regexp dumb. Could you elaborate a little on this. How should this command work?highend wrote:If Don could provide a regexsearch() command, which only stores found patterns in variables, it would be a simple script...
FAQ | XY News RSS | XY X
-
highend
- Posts: 14956
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: rename files including wildcards
E.g.:I'm really regexp dumb. Could you elaborate a little on this. How should this command work?
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;
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).aaa - sbbecc - xxx.txt
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
Yes, from Marco I think.highend wrote:Afaik there was already a suggestion to implement a new regex command that doesn't replace at the same moment.
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.
FAQ | XY News RSS | XY X
-
Stefan
- Posts: 1360
- Joined: 18 Nov 2008 21:47
- Location: Europe
Re: rename files including wildcards
Hi Mike, welcome.mewallis7 wrote:How do I do batch file renames using search and replace with wildcards. Example:
old name * S??E?? *.*
new name * - s??e?? - *.*
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";
}
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
BTW, over at ReNamer we have the following script command "SubMatchesRegEx",
maybe this is from interest here too?
Basically that's:
$PARTS = SubMatchesRegEx( $input, "regex pattern" );
Returns an array of matching groups: $PARTS_1, $PARTS_2, ...
.
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
Sure. It's just a suggestion. Would make things much easier for the user, not for youThis is not supported currently and not easy to do. That would be a major task for me.
<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;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
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]
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
Marco wrote:Simple one liner, works hereCode: Select all
regexreplace ("File Name part S12E34 Trailing Part.ext","S(\d)(\d)E(\d)(\d)","s$1$2e$3$4")
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");
XYplorer Beta Club