Trouble with using substr?!

Advanced Renamer forum
#1 : 29/12-21 18:31
Franz-Georg Neurieser
Franz-Georg Neurieser
Posts: 2
Hello out there,

I tried to rename files which look like

EJSA_631001_20.jpg - where position 8 to 9 holds the year and 10 to 11 the month
EJSA_670001_20.jpg

to

EJSA_631001-2010-01.jpg - adding year, '-' and month to the filename
EJSA_670001-2000-01.jpg

Therefore I tried using a script as follows:

function(index, item) {
var Year=<Substr:8:2>;
var Mon=<Substr:10:2>;
return item.newName + Year + '-' + Mon;
}

When trying to run the script I get the error message: Invalid script: uncauhgt: 'parse error line(1)'

In a second try I used the following script:
function(index.item) {
var oName = item.name;
var Year=oName.Substr(8,2);
var Mon=oName.Substr(10,2);
var nName = item.name + Year + '-' + Mon;
return nName;
}

Now I got the message: Invalid post script: uncauhgt: 'undefined not collable (property \x27Substr\x27 of \x27EJSA_631001_20\x27)'

Any suggestion how to resolve my problem would be great.

Thanks in advance
Franz-Georg


29/12-21 18:31 - edited 29/12-21 19:03
#2 : 30/12-21 00:49
David Lee
David Lee
Posts: 1125
Script 1:

To use Tags in a script you need the function app.parseTags()
See https://www.advancedrenamer.com/user_guide/examp le_scripting
Also item.newName will return name+extension - so you need to use item.newBasename here

var Year = app.parseTags('<Substr:8:2>');
var Mon = app.parseTags('<Substr:10:2>');
return item.newBasename + Year + '-' + Mon;

Script 2:
JavaScript is case sensitive and the .substr method is all lower case
Also, unlike the <Substr> tag, the .substr method enumerates the string position starting from zero

var oName = item.name;
var Year=oName.substr(7,2);
var Mon=oName.substr(9,2);
var nName = item.name + Year + '-' + Mon;
return nName;

Note that item.name returns the ORIGINAL filename (ie as in the list) without extension.

In contrast item.newName and item.newBasename return the new name of each file on entry to the script (ie as changed by any previous methods).


30/12-21 00:49
#3 : 30/12-21 06:51
Franz-Georg Neurieser
Franz-Georg Neurieser
Posts: 2
Reply to #2:
@David Lee,

thanks a lot, you made my day.

Happy New Year

best regards
Franz-Georg


30/12-21 06:51