Conditional zeros into filename / conditional number padding

A swapping-ground for Regular Expression syntax

Conditional zeros into filename / conditional number padding

Postby Pussgurkan » Sat Mar 16, 2024 11:34 am

Hi,

I am a newbie to BRU

Can somebody please help me with this problem?

I have a large number of files that starts with the letter D and look like this
I have also a large number of files that starts with the letter A in the same folder and looks the same.

D1_XXXXXX
D2_XXXXXX
D10_XXXXX
D11_XXXXXX
D100_XXXXXX
D101_XXXXX
D1000_XXXXX
D1001_XXXX

I would like to change the filenames for both category A and D to this

D0001_XXXXXX
D0002_XXXXXX
D0010_XXXXX
D0011_XXXXXX
D0100_XXXXXX
D0101_XXXXX
D1000_XXXXX
D1001_XXXX
Pussgurkan
 
Posts: 2
Joined: Sat Mar 16, 2024 11:20 am

Pad names like 'A'Digits_text or 'D'Digits_text to 4-digits

Postby Luuk » Sat Mar 16, 2024 7:45 pm

With RegEx(1) having a checkmark for "v2", the "Match" and "Replace" can be like...
^([AD])(?=\d+_.+)(?X)(\G(?!^)|^[AD])\K0(?=\d{4,}_.+)/g
${1}000(?X)

A1_text ----------> A0001_text
D5_text ----------> D0005_text
D100_text -------> D0100_text
D000001_text ---> D0001_text
D100001_text ---> (not renamed)
AD10_text -------> (not renamed)

The 1st-regex inserts 3-zeros inbetween A-or-D (at the beginning) and the following digits.
The 2nd-regex deletes any extra leading-zeros (if there is 4-or-more before the underscore).
Related Posts:
http://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=12&t=6376
http://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=3&t=6100
Luuk
 
Posts: 706
Joined: Fri Feb 21, 2020 10:58 pm

Re: Conditionel zero into filename

Postby Pussgurkan » Sun Mar 17, 2024 7:23 pm

Many Thanks!
Your solution will make many people happy in Sweden
Pussgurkan
 
Posts: 2
Joined: Sat Mar 16, 2024 11:20 am

Re: Conditionel zero into filename

Postby Admin » Mon Mar 18, 2024 1:59 am

Hi, out of interest, here is a BRU javascript that does the same for Special (14):

function padToFour(number) {
if (number<=9999) { number = ("000"+number).slice(-4); }
return number;
}

const separator = "_";

var sl = newName.substring(0, 1);
if(sl == "A" || sl == "D") {
var num = newName.substring(1,newName.indexOf(separator));
var ends = newName.substring(newName.indexOf(separator));
newName = sl + padToFour(num) + ends;
}
Admin
Site Admin
 
Posts: 2354
Joined: Tue Mar 08, 2005 8:39 pm


Return to Regular Expressions