RegExp batch rename

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
pixelsearch
Posts: 43
Joined: 21 May 2018 18:45

RegExp batch rename

Post by pixelsearch »

Hi everybody
I got a folder containing the following files :

filename (1).txt
...
filename (9).txt
filename (10).txt
...
filename (19).txt

Using XYplorer, I wanted to batch rename some files so (1) to (9) become (01) to (09)
This is the menu option and the pattern I tried : File => Rename Special => RegExp Rename...
(.*\()(\d\)\..*) > $10$2

It seems to work and filename(1).txt became filename(01).txt ... filename(9).txt became filename(09).txt ... other files names remained untouched (as they already got 2 digits)

Could you please share your opinion about this, in case there is another way to do it ?
Thanks

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

Re: RegExp batch rename

Post by highend »

There are dozen of ways to do this. Why does it matter if it works?
(?:\()(\d)(?:\)) > (0$1)
One of my scripts helped you out? Please donate via Paypal

pixelsearch
Posts: 43
Joined: 21 May 2018 18:45

Re: RegExp batch rename

Post by pixelsearch »

Thanks highend for your input.
As it took me time to find the RegExp solution, I was asking in case there was a simpler option in another XPlorer Rename menu, without RegExp.

Anyway as it works fine, I'm gonna keep those patterns carefully because this specific batch Rename is needed from time to time. Gonna add them in a reminder file I already got, named "RegEx characters to be escaped (13).txt", which contains the following lines (I had to open that file before trying and finding the RegExp solution, as I'm a newbie when it comes to RegExp) :

\.^$|[({*+?#)
Above are the 13 RegEx metacharacters to be escaped, for example :
a dot . will be escaped to \.
a star * will be escaped to \*
etc...

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

Re: RegExp batch rename

Post by highend »

A # is not a regex metacharacter

And you can reduce your set for files / paths to 9.
|, ? and * can't be a (officially) part of them (at least not under NTFS filesystems)
One of my scripts helped you out? Please donate via Paypal

pixelsearch
Posts: 43
Joined: 21 May 2018 18:45

Re: RegExp batch rename

Post by pixelsearch »

Hi highend
Thanks for the clarification. Just a question :
If # isn't a regex metacharacter, why does it need to be escaped in the following test ?

Match text:
#abcde

Search pattern:
(?x)# introduces a comment, next # must be escaped
\#abc

Result:
#abc

Without escaping it, the result is wrong. Maybe # should be considered as a metacharacter as soon as we're dealing with eXtended (?x)

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

Re: RegExp batch rename

Post by highend »

Mode modifiers in VB6? They aren't supported^^
One of my scripts helped you out? Please donate via Paypal

Post Reply