Rename script, formating the result file name

I'm using the following rename script for e.g. this example filename:

Konto_0320435115-Auszug_2022_0008.PDF

function OnGetNewName(getNewNameData)
{
	var name = getNewNameData.newname_stem_m;
	var ext = getNewNameData.newname_ext_m;
	//replace underscores with space
	//move date from last position to first and remove parts
	name = name.replace( /_/g ," " )
			.replace(/(.*)( vom| zum| per) (\d{2}) (\d{2}) (\d{4})/,'$5-$4-$3 $1')
			.replace(/(Konto) (\d.*)-(Auszug) (\d{4}) (\d{3,4})/,'$4 $1$3 $2 #$5')
			.replace(/ae/g,"ä")
			.replace(/Ae/g,"Ä")
			.replace(/oe/g,"ö")
			.replace(/Oe/g,"Ö")
			.replace(/ue/g,"ü")
			.replace(/Ue/g,"Ü")
			.replace(/Michäl/g,"Michael")
			;
	
//	name = name.replace(/(.*)( vom| zum) (\d{2}) (\d{2}) (\d{4})/,'$5-$4-$3 $1');
	return name + ext;
}

Which gives this result after applying:

2022 KontoAuszug 0380445445 #0008.PDF

Is there any chance to get "Auszug" into lower case so the complete word reads "Kontoauszug", with the above script?

Of course I could split this rename line
.replace(/(Konto) (\d.*)-(Auszug) (\d{4}) (\d{3,4})/,'$4 $1$3 $2 #$5')
into different parts to achieve this, but would like to have this in one line.

I might be missing something here, but on the line :

.replace(/(Konto) (\d.*)-(Auszug) (\d{4}) (\d{3,4})/,'$4 $1$3 $2 #$5')

you are exactly matching the Auszug part and are copying it as $3 in the replace pattern.
If you want it lower case, just transform this line into :

.replace(/(Konto) (\d.*)-(Auszug) (\d{4}) (\d{3,4})/,'$4 $1auszug $2 #$5')

Did not test, you might have to put ${1}auszug.
EDIT : Tested, no need for extra bracket, first suggestion should give you what you expect.
EDIT2 : Same thing goes for the Konto part which is exactly matched, so you could even replace by :

.replace(/(Konto) (\d.*)-(Auszug) (\d{4}) (\d{3,4})/,'$4 Kontoauszug $2 #$5')

EDIT3: :slight_smile: even simpler, no need to catch Konto and Auszug in groups:

.replace(/Konto (\d.*)-Auszug (\d{4}) (\d{3,4})/,'$2 Kontoauszug $1 #$3')

Yes, sure I can put the desired, final part of the filename into the rename command :laughing: :wink:
However, I would like to use the regex groups without using a static name, "Kontoauszug" in this case.

I thought about sthg. like

.replace(/(Konto) (\d.*)-(Auszug) (\d{4}) (\d{3,4})/,'$4 $1 {=LCase($3)=} $2 #$5')

using an evaluator function.

Since you are exactly matching these parts of the filename, capture groups are useless. Capture groups are interesting when you don't know exactly what you're matching (e.g. the \d.* or \d{3,4} ...).

Yes, I know of course, but I tried to learn about rename scripts and found it handy to use regex groups within the .rename function :grimacing:

So, you mean it makes my life simpler when dropping regex groups in favour of some simpler instructions?

Yes, when it's not necessary.
In that regexp, you are using purposefully 3 catching groups already.

1 Like

There is no case conversion in JScript's regex, and it certainly can't interact with Opus' Evaluator.

The pragmatic approach would be a simple string replace, but no one will stop us from using replace with a function :sunglasses:

.replace(/(Konto) (\d.*)-(Auszug) (\d{4}) (\d{3,4})/, function (match, p1, p2, p3, p4, p5) {
    return p4 + ' ' + p1 + p3.toLowerCase() + ' ' + p2 + ' #' + p5;
})
2 Likes

Works :wink: Thanks for this gem!