Incrementing a file number by basename

Hi all,

Since we're talking numbers and renumbering, I thought I'd throw this out there. There has been a lot of interest over the years in incrementing numbers on filenames (like <IncNr> and <Inc NrDir>), but restarting the numbering at 1 when the base filename changes. Here's an example of that back in maybe 2021:

https://www.advancedrenamer.com/forum_thread/continuous-numb ers-11754

I've written a little function in script that does this. It's a function in pre-batch, so it could be saved as a discrete method batch and just appended as the last method to another batch when needed - using version 4's new "Append batch" command in the batch list drop-down. Unless you want a different separator character you'd never have to touch it once installed and saved (but if you do it's easy to change).

Here's the script:
//------------------------------------------------
// PREBATCH
// These vars are necessary for f_IncNrBasename() (but must be outside it):
let sameNameCounter = 1 ;
let previousName = "" ;
var newName = "" ;

function f_IncNrBasename( originalName, numberPadChar ) {// supply original name, character(s) to
// put between name and number (default is "_")
// CHANGE THE FOLLOWING LINE for different default pad:
numberPadChar = ( ! numberPadChar ? "_" : numberPadChar ) ;

if ( index == 0 ) {
// This is the first filename; we add the pad + 1 (sameNameCounter):
newName = originalName + numberPadChar + sameNameCounter.toString() ;
// Increment counter:
sameNameCounter++ ;
// and assign this file's original name to the variable that stores the previous filename:
previousName = originalName ;
} else {
// Not the first filename:
if ( previousName == originalName ) {
// This filename keeps the name of the previous file, so add the incremented counter:
newName = originalName + numberPadChar + sameNameCounter.toString() ;
// Increment the counter:
sameNameCounter++ ;
} else {
// we've changed base filenames; reset counter:
sameNameCounter = 1 ;
// add pad + counter:
newName = originalName + numberPadChar + sameNameCounter.toString() ;
// NOW increment counter for next file:
sameNameCounter++ ;
// And change the previous filename storage var to the current base name:
previousName = originalName ;
}
}
return newName ;
}
//------------------------------------------------


And the main script part:
//------------------------------------------------
// IncNrBase.js.aren
// This little script will add an incrementing number (and your choice of pad)
// to each filename, resetting to 1 when the base name changes. You'll need to have
// "Pair renaming" set OFF for this to work. I recommend to save as separate batch,
// then use version 4's "Append method list" capability to add it to a method batch
// at the end of the batch when needed.

// MAIN
return f_IncNrBasename( item.newBasename ) ;
//------------------------------------------------

To set it up just open a script method (blank it if needed), paste the pre-batch into the pre-batch part and the main into the main part. Click "Apply script", save it (I use IncNrBase.js.aren), and then load or append it as needed. If you want to temporarily change the "numberPadChar" from the default underscore, just change the one line in the main part to (assuming a space):
return f_IncNrBasename( item.newBasename, " " ) ;

To permanently change it go to pre-batch and change the
numberPadChar = ( ! numberPadChar ? "_" : numberPadChar ) ;
(line 2 of code), replacing "_" with whatever character(s) you want in quotes.

I hope this is useful to ya... I haven't tested it much so if you find a problem let me know.
screenshot: https://drive.google.com/file/d/1qVyzOSx9Sq7QdfRIAspqyrxikKl PDPaa/view?usp=sharing
Best,
DF