addendum to "swapping parts of a filename" thread

see title... I get an error either way

Now of course it let me post this... (EDIT: it was a stupid windows emoticon that I assumed was text-friendly)

Here's what I tried to post before on David Thompsons "Swapping parts of a filename" thread:

(for some reason I get an error trying to add this to the original thread... hopefully this will post)

Reply to #6:

Ok, I admit it... I was so giddy to have gotten an interesting question on this forum (it's been veeery quiet lately) that I couldn't help myself... hence all the answers above. Here's a (not-ultimate) javascript :) version:

/// ---------------- swap last two words in filename if last word is an 8-char date ----------//

function last2WordsLen( str ) {
let splitArr = str.split(" ") ;
let splitLen = splitArr.length ;
return splitArr[splitLen - 2].length + 1 + splitArr[splitLen -1].length ;
}

let n = item.newBasename ;
let nLen = n.length ;
let nArr = n.split(" ") ;
let nWords = nArr.length ;
// if less than 2 words return unchanged:
if ( nWords < 2 ) {
return ;
}

let lastWord = nArr[nArr.length - 1] ;
// Check for valid date, if not return unchanged:
let dateOK = /\d{8}/.test( lastWord ) ;
if ( ! dateOK ) {
return ; // There could be a fixer function here
// to straighten out a date if in another format, of course...
}

let last2Len = last2WordsLen( n ) ;
let newBase = n.slice( 0, nLen - last2Len ) ;
return newBase + nArr[nArr.length - 1] + " " + nArr[nArr.length - 2 ] ;
// ---------------------------------------------------------------------------------------------//

The function could go in Pre-batch; but for less than thousands of files on a slow disk drive it's not really necessary.
Ok, shoot me, I got a little carried away!

Best,
DF