Page 1 of 1

Regex rename template

Posted: 19 Dec 2014 05:04
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

Re: Regex rename template

Posted: 20 Dec 2014 12:51
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.

Re: Regex rename template

Posted: 28 Nov 2020 09:45
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.

Re: Regex rename template

Posted: 28 Nov 2020 09:58
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?

Re: Regex rename template

Posted: 29 Nov 2020 20:34
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).

Re: Regex rename template

Posted: 29 Nov 2020 20:37
by highend

Code: Select all

    foreach($item, <get SelectedItemsPathNames |>, , "e") {
        renameitem(regexreplace(replace(gpc($item, "base"), ".", " "), "(\d{4})$", "($1)") . "." . gpc($item, "ext"), $item);
    }

Re: Regex rename template

Posted: 01 Dec 2020 02:43
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 :(

Re: Regex rename template

Posted: 01 Dec 2020 06:31
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);
        }
    }