Change Case with optional "Exception List"

Advanced Renamer forum
#1 : 27/04-21 10:13
arwul
arwul
Posts: 94
Quite a while ago there was a thread titled "Suggestion for Case Change function - Exception Lists".

Maybe I am overlooking it, vainly searched for it. Maybe it has indeed been implemented, I don't know.

If not, I would like to revive that thread and bring it up again.
Suggestion to add a few predefined lists (available on Internet) and a user defined list (.txt) that should reside in the Advanced Renamer folder.

Predefined, for example:
1. Computer (CD DVD MP3 MP4, etc..)
2. Countries (USA, UK, etc.)
3. User defined (Like: AD;DC;X1;A5;WD;WF, etc.), with or without leading and/or trailing underscore or hyphen, e.g. -AD- or _AD etc.

Keep up the good work!



=


27/04-21 10:13
#2 : 27/04-21 19:44
David Lee
David Lee
Posts: 1125
Certainly was "quite a while ago" - nearly 8½ years ago in fact!

The search facility on this forum is pretty rubbish - its better to use an advanced Google search specifying site:www.advancedrenamer.com

The post you refer to is www.advancedrenamer.com/forum_thread?forum_id=1069

However you can achieve what you want using a Script method reading your lists from a text file

For example to change the case of selected words to all upper case...

Save your list of words to a text file, with one word per line

eg list.txt:

cd
dvd
mp3
mp4
etc

In the Pre batch script:

var words = [];
list = "C:\\Users\\username\\full path\\list.txt";
list = '::\"' +list + '\"';
j=0;
while (words[j] = app.parseTags('<File Line:' + (++j) + list + '>'));

and in the main script window:

name = item.name;
words.forEach(function(word){
regex = RegExp('( |^)' + word + '( |$)',"gi");
name = name.replace(regex, function($1) {
return $1.toUpperCase();
});
})
return name;

Modify the path to list.txt appropriately, noting that you must replace all instances of "\" with "\\"


27/04-21 19:44 - edited 27/04-21 19:52
#3 : 29/04-21 07:50
arwul
arwul
Posts: 94
Reply to #2:
Thank you very much indeed!
Yes, the initial thread was a long time ago. There aren't many renamers supporting an exception list.
OTOH I would assume that it is not desirable to change the case of abbreviations in file names.
Like: CD or DVD
so: blah blah CD should be changed to : Blah Blah CD and not Blah Blah Cd

As for the below, frankly, I believe scripts are for the real experts. Actually it would be much easier to be able to just tag 'Use exception list' and such list should be a simple .txt file.

Even having a script available, like yours, one still needs to know how to get it into Advanced Renamer and apply it...

Anyway, in the below example, your last line, you mean to say that
list = '::\"' +list + '\"';
should be changed into
list = '::\\"' +list + '\\"';

?


29/04-21 07:50
#4 : 29/04-21 10:06
David Lee
David Lee
Posts: 1125
Reply to #3:
Kim has added the Script method in order to allow you to use Advanced Renamer beyond its built-in capabilities. Whilst he does respond to requests for new features it would be unreasonable to expect him to add every feature that a user may wish for.

Simple scripting does not require any great expertise - I had no experience of either JavaScript or Regular Expressions until I started using this program. The information in the User Guide was sufficient to get me started after which Internet resources allowed me to progress.

It looks as though you haven't tried to use the scripts I gave you - using them in Advanced Renamer is a simple matter of pasting the text into the pre batch and main script windows of the Script method.

Regarding the "\" character in strings: This is the "escape" character, used to modify the meaning of the following character in the string - either to give it a special meaning or else to cancel a default special meaning. For example "t" will return the character 't' whereas "\t" will return a tab character. Thus for "\" to represent itself it has to be "escaped" as "\\".

In JavaScript a quote character has the special meaning of indicating the beginning or the end of a string, so in order to insert a quote in a quoted string it must be escaped. Hence "list = '::\"' +list + '\"';" is correct.

However in this case the escape characters are not strictly necessary so "list = '::"' +list + '"';" would also be acceptable. This is because you can use either single or double quotes to define a string in JavaScript. A double-quoted string can contain single quotes without escaping them and vice versa. However this is open to confusion and it is safer to add the escape character.


29/04-21 10:06 - edited 29/04-21 10:12
#5 : 29/04-21 10:54
arwul
arwul
Posts: 94
Reply to #4:
Thanks again. Whilst far, far away from being an expert, I do know something about Regex.
Scripts, that is different cook.
I don't know anything about scripts.

I did try your script, but was not sure about the \ stuff.

the 2nd line (+list+)

I tried with \\ with single \ without \ using the word +list+ or + exceptions.txt

The last version (not working) is

see: https://imgur.com/qg2drDH

-
var words = [];
list = "D:\\Advanced Renamer\\Exceptions.txt";
list = '::\"'+Exceptions.txt+'\"';
j=0;
while (words[j] = app.parseTags('<File Line:' + (++j) + list + '>'));

and in the main script window:

name = item.name;
words.forEach(function(word){
regex = RegExp('( |^)' + word + '( |$)',"gi");
name = name.replace(regex, function($1) {
return $1.toUpperCase();
});
})
return name;

-
I end up with
Invalid script: Invalid input pre script: uncaught: 'unterminated statement (line 7)'



29/04-21 10:54
#6 : 29/04-21 13:16
David Lee
David Lee
Posts: 1125
Reply to #5:

Read the User Guide then follow my instructions sensibly!

www.advancedrenamer.com/user_guide/method_script
www.advancedrenamer.com/user_guide/example_scripting

It should be obvious that my comments "In the Pre batch script:" & "in the main script window:" tell you where to put your code.

The first block of code should be entered in the "Pre batch script" window and runs once only to read the data from your txt file.

The second block goes into the main window of the Script method and runs for each filename in the list.



29/04-21 13:16