Change filename with script?

I'm currently renaming my comic library with lots of numbers and character in filenames, however, they always ending with p01 p02 p03...

Here is my current filenames: 189239427a3cw_p01.jpg, 189239427a3cw_p02.jpg, 189239427a3cw_p03.jpg, 189239563a3dz_p01.jpg, 189239563a3dz_p02.jpg, 189239563a3dz_p03.jpg, 189239563a3dz_p04.png, 190846122b2eb_p01.png, ... (~650 700 more)

So my expected filenames should be: 0001_p01.jpg, 0001_p02.jpg, 0001_p03.jpg; 0002_p01.jpg, 0002_p02.jpg, 0002_p03.jpg, 0002_p04.png; 0003_p01.png...

Those files are in the same folder.

How to do that?

Many thanks
Reply to #1:

Hi Onion,

Fun stuff. I thought I could trick ARen using collision rule numbers, but ended up making this little script instead:

>>>>>NOTE: When you run this you must have your files sorted on Filename ascending.

(In Pre-batch script:)-------------------------------------------------------

const prevSeriesStr = "";
const prevSeriesNr = 0;
const prevName1 = "";

---------------------------------------------------------------------------
(Script to process each filename:)

//replace series with sequentially-numbered series:

// WARNING: If there are more than 99 different comic books, or 99 different pages in
// one comic book, this may not work correctly.
// Definitely if there are more than 99 comic books.

// WARNING 2: Make sure files are sorted on filename, ascending.
// -------------------------------------------------------------------

//The following constants go in Pre batch script.
//const prevSeriesStr = ""; <- to track the last instance of ORIGINAL part before underscore
// for comparison
//const prevSeriesNr = 0; <- to track the number we'll be converting to padded string
// for comparison
//const prevName1 = ""; <- to track the previous NEW NAME for comparison.
//----------------------------------------------------------

oldName = item.newBasename.split("_");
oldName1 = oldName[0]; // Part to replace

newName2 = oldName[1]; //Part that stays for new filename

if (oldName1 != prevSeriesStr) {
// Series increments here. Make padded new series and update Pre batch constants:
prevSeriesNr += 1;
prevSeriesStr = oldName1;
// Zero-pad the new string correctly:
newName1 = ( prevSeriesNr - 9 >=0 ) ? "00" + prevSeriesNr.toString() : "000" + prevSeriesNr.toString();
prevName1 = newName1;
} else {
// Still the same seris, so pick up value from constant:
newName1 = prevName1;
}
// Booyah!
return newName1 + "_" + newName2;

---------------------------------------------------------------------------
Sorry about all the comments, that's the way I work.

here's what I got with your sample set:

https://drive.google.com/file/d/1ptyiler6jTieg0a3VLC7aeZIXAi xahXc/view?usp=sharing

Best regards,
DF

Reply to #2:

Hi DF,

Thanks for promptly response, will give this a try when I get home and give you the result asap :)

Edit: It works wonderfully, many thanks for your help.

Sincerely,

Onion