Change numbers in file name to pad4 with text either side

Bulk Rename Utility How-To's

Change numbers in file name to pad4 with text either side

Postby Mick02 » Mon Aug 21, 2023 2:14 pm

Sorry if this has already been answered but I cannot seem to find an answer to suit my situation. For example, I have files named "m20 - SHAFT - Rev D - For Fabrication.pdf". The text can differ but there are always only 1 or 2 letters at the start & the number following the first or second letter can be a 1, 2, 3 or 4 digit number. I would like to change the number so it is 4 digits long. So, for the example, it would end up as "m0020 - SHAFT - Rev D - For Fabrication.pdf". Any help would be greatly appreciated.
Mick02
 
Posts: 4
Joined: Mon Aug 21, 2023 2:10 pm

Re: Change numbers in file name to pad4 with text either side

Postby therube » Mon Aug 21, 2023 5:59 pm

I would imagine that could be done with JavaScript renaming (pay feature)?

Otherwise, possible (?) RegEx: could do it (but if so, I don't know how)?

1:RegEx
Code: Select all
Match:  ([a-z]+)(\d+) ...
Replace: \1\2

\1 would cover your 'letters'
\2 would cover your numbers

Now how to (or if you can actually) "pad" the number part (using RegEx)... ?
therube
 
Posts: 1319
Joined: Mon Jan 18, 2016 6:23 pm

Re: Change numbers in file name to pad4 with text either side

Postby Mick02 » Mon Aug 21, 2023 11:47 pm

Hi therube,
Thanks for the reply. I was playing with a similar setup to what you show but cannot get the pad 4 to show what I want. I'll try your way & see what I get. Thanks again.
Mick02
 
Posts: 4
Joined: Mon Aug 21, 2023 2:10 pm

Pad numbers within prefix to 4-digits

Postby Luuk » Tue Aug 22, 2023 5:45 am

This depends on your prefixs starting with 1-or-2 english letters, followed by 1-3 digits, followed by " - ", and then some text.
RegEx(1) can pad them to 4-digits, but needs a checkmark for "v2", with a "Match" and "Replace" something like...
^([a-z]{1,2})(\d{1,3} - .+)(?X)(\G|^[a-z]{1,2})\K0(?=\d{4,} - .+)/g
${1}0000000000$2(?X)

With the checkmark for "v2", RegEx(1) can use (?X) to conduct many different regexs during 1-rename.
So the 1st-Match/Replace just adds many extra zeros, but really all your Replace needs is just 3-zeros.
The 2nd-Match doesnt have any Replace, so it deletes any extra zeros, when there's 4-or-more digits.
Luuk
 
Posts: 706
Joined: Fri Feb 21, 2020 10:58 pm

Re: Change numbers in file name to pad4 with text either side

Postby Mick02 » Tue Aug 22, 2023 7:28 am

Hi Luuk,
Thanks for that I'll give it a try.
Mick02
 
Posts: 4
Joined: Mon Aug 21, 2023 2:10 pm

Re: Change numbers in file name to pad4 with text either side

Postby Mick02 » Tue Aug 22, 2023 7:36 am

Hi Luuk,
I just tried your solution on a couple situations & it works well.
Thanks again.
Mick02
 
Posts: 4
Joined: Mon Aug 21, 2023 2:10 pm


Return to How-To