Replacing Multiple Names with Abbreviations

Advanced Renamer forum
#1 : 26/03-15 01:46
Raymond
Raymond
Posts: 3
Hi All,

This is my first posting, so please be kind:)

I have a situation where I need to replace the creator name initials contained in a bunch of file names with their corresponding full names. For instance:

SomeTextDoc1_pp.docx
SomeDoc2_ck.docx
SomeLongTextDoc3_bw.docx

change to:

SomeTextDoc1_peterparker.docx
SomeDoc2_clarkkent.docx
SomeLongTextDoc3_brucewayne.docx

You can assume that each initial is only paired with a single full name.

Do I need to write a script to do this? If so, how?

Thanks.


26/03-15 01:46
#2 : 26/03-15 12:07
NomenEtOmen
NomenEtOmen
Posts: 29
Reply to #1:
you have to do the mapping, so a script is a good idea.
Here a prototype (Apply to "Name"):


Function (index,item) {

var myMap = {
"pp":"Peter Parker",
"ck":"Clark Kent",
"bw":"Bruce Wayne",
"jw":"John Wayne"
};

var nName = ""; // new name
var oName = item.name; // original name

//isolation of key
var iSep = oName.lastIndexOf("_");
if (iSep<0) {
//no sep found -> exit
return;
}
var sKey = oName.substr(iSep+1);


//getting replacement
nName = myMap[sKey];
if (nName == undefined || nName == null) {
//key not found in map -> exit
return;
}

//building new name
nName = oName.substring(0,iSep+1)+nName;

//... some further things


//end: returning new name
return nName;

----------
neo


26/03-15 12:07 - edited 26/03-15 12:16
#3 : 28/03-15 20:50
Raymond
Raymond
Posts: 3
Reply to #2:

Thanks! This works as expected.

Follow-up questions:
1) I forgot to mention that the initials can vary in length. For instance, "bho" for "Barak Hussein Obama". I'm thinking that a counter variable needs to be set for this, but not sure how. Would you mind to show how this is done?

2) Also, in the case where initials appear at the beginning of the file name (e.g. bho_SomeFileDoc.docx"), what would need to be adjusted?



28/03-15 20:50
#4 : 29/03-15 15:20
NomenEtOmen
NomenEtOmen
Posts: 29
Reply to #3:
fine that it helps.

your requests:

1) I forgot to mention that the initials can vary in length. For instance, "bho" for "Barak Hussein Obama". I'm thinking that a counter variable needs to be set for this, but not sure how. Would you mind to show how this is done?

length is irrelevant :-)
the script splits the fileName at the last _ and then checks if the end-standing part is mapped in the dictionary independent from it's length.


2) Also, in the case where initials appear at the beginning of the file name (e.g. bho_SomeFileDoc.docx"), what would need to be adjusted?

in this case you have to split at the first _ and use the left-standing part for looking up in the dic.
also the replacement varies: you have to change at the start of the name-string.



a combinated script for this:
- it first tests on end-standing replacement
- if NOT successful, it tests on left-standing replacement
- both uses separator _
- length is irrelevant




var myMap = {
"pp":"Peter Parker",
"ck":"Clark Kent",
"bw":"Bruce Wayne",
"jw1":"John Wayne",
"test":"TestString"
};

var nName = ""; // new name
var oName = item.name; // original name

//isolation of last-standing key
var iSep = oName.lastIndexOf("_");
if (iSep<0) {
//no sep found -> exit
return;
}

//substitute
var sKey = oName.substr(iSep+1);
nName = myMap[sKey];
if (nName == undefined || nName == null) {
//key not found in map -> exit
//return;
} else {
//found
//building new name
nName = oName.substring(0,iSep+1)+nName;
//end: returning new name
return nName;
}


//not yet found: test left-standing part:
//isolation of left-standing key
var iSep = oName.indexOf("_");
if (iSep<0) {
//no sep found -> exit
return;
}

//substitute
var sKey = oName.substr(0,iSep);
nName = myMap[sKey];
if (nName == undefined || nName == null) {
//key not found in map -> exit
//return;
} else {
//found
//building new name
nName = nName + oName.substring(iSep);
//end: returning new name
return nName;
}


----------
neo


29/03-15 15:20
#5 : 31/03-15 10:56
Raymond
Raymond
Posts: 3
neo, it works beautifully! You're truly awesome.

Thanks again!


31/03-15 10:56