Rename JS from XXX002XXX to XXX000XXX

Javascript renaming examples. Javascript renaming is supported in version 3 or newer.

Rename JS from XXX002XXX to XXX000XXX

Postby kotonier » Sat Jun 11, 2022 8:38 pm

Hello,

I hope everyone is doing well!

Unfortunately, my JS knowledge is near 0.

I'd like to rename more than 1,000 files.

All got the same syntax, like Abcdefg016hij

I'd like to rename

Abcdefg016hij to Abcdefg000hij
Abcdefg017hij to Abcdefg001hij

and so on. Is there a way to do it?

Thank you for your answers
kotonier
 
Posts: 1
Joined: Sat Jun 11, 2022 8:23 pm

Subtract 16 from files with only 1-number in the middle

Postby Luuk » Mon Jun 13, 2022 9:42 pm

http://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=2&t=4849#p13292
This a link using the javascript to add 559, but its also only renaming the files that do end with a number.
So this a modification to rename files with having only 1-number in the middle, but at least 3-digits long.
Also, the number must be at least 016, since because its always subtracting 16 away from the middle number...

if (/^[^\d]+(?!0(0\d|1[0-5])[^\d])\d{3,}[^\d]+$/.test(name)) {
beg=name.replace(/^(.*?)\d+.*$/,'$1') //.... beg == Begin of Name
end=name.replace(/^.*?\d+(.*)$/,'$1') //.... end == End of Name
num=name.replace(/^.*?(\d+).*$/,'$1') //... num == Middle digits
xxx=num.length // ............................. xxx == How many Middle digits? (with LeadZeros)
sub=parseInt(num,10)-16 // .................. sub == Middle digits - 16 (in decimal)
yyy=sub.toString().length // .................. yyy == #digits in sub (no LeadZeros)
zer='0'.repeat(Math.max(0,xxx-yyy)) //...... zer == LeadZeros (if needed)
newName=beg+zer+sub+end} // .............. NewName == BeginName + LeadZeros(if needed) + Subtraction + EndName

If all filenames are like the example, then Line-1 can be... if (/^Abcdefg(?!0(0\d|1[0-5]))\d{3,}hij$/.test(name)) {
If needing to subtract 16 only from 3-digits instead of 3-or-more, then should also change \d{3,} ===> \d{3}
Luuk
 
Posts: 771
Joined: Fri Feb 21, 2020 10:58 pm


Return to Javascript Renaming