How to keep the numbering inserted during the last use - TAKE 2?
Not sure why, but I got to thinking about other ways to add an incrementing number in a directory of pictures. This is a pretty simple script that *seems* on limited testing to do that.
//---------------------------------------
// PRESCRIPT
// find the last filename with _nnnnn at end:
let lastName, testName, lastIndex, prevNum ;
for ( j = 0; j < app.itemCount; j++ ) {
testName = app.getItem( j ).name ;
if ( /_\d+$/.test( testName ) ) { // filename has _nnnnn
lastName = testName.replace( /.*_0*(\d)$/, "$1") ; // grab the current number from _nnnnn
prevNum = parseInt( lastName ) ; // make it a number
lastIndex = app.getItem( j ).index ; // grab index number of last filename with _nnnnn
}
} // we have the information to know where to start numbering filenames...
//---------------------------------------
// MAIN
let n = item.newBasename ;
// let newNum ;
if ( lastIndex < index ) { // we are in unmarked territory
++prevNum ; // incr prevNum
n = n + "_" + prevNum.toString().padStart( 5, "0" ) // add to filename
}
return n ;
//---------------------------------------
A couple caveats:
1. It doesn't test for file-type, so load only the files (jpg?) that you want. Or add logic to exclude whatever you don't want to change.
2. The already-numbered files must come first. Easy enough to engineer...
3. If you move the numbered files out of the working directory, leave the last numbered file as a "starter".
4. I used an underscore as the number separator, out of habit. Just replace the underscore with whatever character(s) you want. (it's in the 3rd- or 4th-to-last line, depending how you count - look for `n = n + "_" +`)
Just a thought exercise for my old dog-brain.
Best,
DF
//---------------------------------------
// PRESCRIPT
// find the last filename with _nnnnn at end:
let lastName, testName, lastIndex, prevNum ;
for ( j = 0; j < app.itemCount; j++ ) {
testName = app.getItem( j ).name ;
if ( /_\d+$/.test( testName ) ) { // filename has _nnnnn
lastName = testName.replace( /.*_0*(\d)$/, "$1") ; // grab the current number from _nnnnn
prevNum = parseInt( lastName ) ; // make it a number
lastIndex = app.getItem( j ).index ; // grab index number of last filename with _nnnnn
}
} // we have the information to know where to start numbering filenames...
//---------------------------------------
// MAIN
let n = item.newBasename ;
// let newNum ;
if ( lastIndex < index ) { // we are in unmarked territory
++prevNum ; // incr prevNum
n = n + "_" + prevNum.toString().padStart( 5, "0" ) // add to filename
}
return n ;
//---------------------------------------
A couple caveats:
1. It doesn't test for file-type, so load only the files (jpg?) that you want. Or add logic to exclude whatever you don't want to change.
2. The already-numbered files must come first. Easy enough to engineer...
3. If you move the numbered files out of the working directory, leave the last numbered file as a "starter".
4. I used an underscore as the number separator, out of habit. Just replace the underscore with whatever character(s) you want. (it's in the 3rd- or 4th-to-last line, depending how you count - look for `n = n + "_" +`)
Just a thought exercise for my old dog-brain.
Best,
DF