Regex rename template

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
binocular222
Posts: 1424
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Regex rename template

Post by binocular222 »

Please contribute some fancy Regex pattern. I start first:
Insert xxx after the 6th character=^(.{6})(.*) > $1xxx$2
Insert xxx before extension=(\.[^.]+)$ > xxx$1
Delete first 3 characters=^(.{3})(.*) > $2
Delete last 3 characters=^(.*)(.{3})(\.[^.]+)$ > $1$3
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

aliteralmind
Posts: 261
Joined: 02 Dec 2014 16:49

Re: Regex rename template

Post by aliteralmind »

For those learning regex, here's the Stack Overflow regular expression FAQ, which I maintain:

http://stackoverflow.com/questions/2293 ... 5#22944075

It's a great reference, and includes a list of online testers and explainers (debuggers) at the bottom.
Windows 8.1, 64-bit

scienide
Posts: 6
Joined: 23 Dec 2016 23:23

Re: Regex rename template

Post by scienide »

How do I insert a character, x number of characters from the end, while also swapping periods for spaces?

So if I have:

Name.of.File.0000.pdf
Is there a way to do one regexp rename or script to change that to this:
Name of File (0000).pdf

?

Basically I want to change periods to spaces, and put brackets around the last 4 digits, all in one go.

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

Re: Regex rename template

Post by highend »

Are these always digits that you want to embrace?
Are these always 4 digits?
Is it possible that others digits are in the file name?
One of my scripts helped you out? Please donate via Paypal

scienide
Posts: 6
Joined: 23 Dec 2016 23:23

Re: Regex rename template

Post by scienide »

Thanks for the reply on such an old thread. Wasn't sure if I'd get one.

Indeed, the 4 digits (year) are always at the end, and I want to add brackets around the year.

There aren't always periods but that's not a big deal actually as I can do a search and replace first to remove any periods, and then perhaps do a RegExp rename to add brackets around the last 4 characters of the filename (right before the extension, like in my example above).

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

Re: Regex rename template

Post by highend »

Code: Select all

    foreach($item, <get SelectedItemsPathNames |>, , "e") {
        renameitem(regexreplace(replace(gpc($item, "base"), ".", " "), "(\d{4})$", "($1)") . "." . gpc($item, "ext"), $item);
    }
One of my scripts helped you out? Please donate via Paypal

scienide
Posts: 6
Joined: 23 Dec 2016 23:23

Re: Regex rename template

Post by scienide »

Thanks, that's awesome!

So I actually lied. I realized I do have quite a few files that I already manually renamed, so is it possible to ignore files that are already named properly? So basically I would want the script to ignore anything that already has brackets around the year and periods removed. Essentially it would just need a check to see that the last character in the filename is a # and not something else like a bracket - or perhaps to make it foolproof, it would look for the whole string (xxxx). It should be perfect then! Thanks again.

PS - The other option might be to run a targeted search to ignore files containing (xxxx) -- where xxxx represents the year -- and then run the script on the search results, but I don't know how to do this either :(

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

Re: Regex rename template

Post by highend »

Code: Select all

    foreach($item, <get SelectedItemsPathNames |>, , "e") {
        $base = gpc($item, "base");
        $ext  = gpc($item, "ext");

        if (!regexmatches($base, "\(\d{4}\)$")) {
            renameitem(regexreplace(replace($base, ".", " "), "(\d{4})$", "($1)") . "." . $ext, $item);
        }
    }
One of my scripts helped you out? Please donate via Paypal

Post Reply