Rename Script With Logic

Advanced Renamer forum
#1 : 22/02-23 16:20
Emily
Emily
Posts: 8
Hi, I have the following script which is working well, but what I would love to do is to move files in the Large Images directory if their filename includes either A1 or B2 or C3. Any advice please?

if (item.name.includes("A1")) item.newPath = item.path + "Large Images";

-

Do I just need to list everyone separately (there are quite a few)?

if (item.name.includes("A1")) item.newPath = item.path + "Large Images";
if (item.name.includes("B2")) item.newPath = item.path + "Large Images";
if (item.name.includes("C3")) item.newPath = item.path + "Large Images";


Also is there a way to move files into a Subdirectory of Large Images at all please?

Apologies that I don't know more about this.

Many thanks,

Emily


22/02-23 16:20 - edited 22/02-23 17:33
#2 : 22/02-23 19:56
David Lee
David Lee
Posts: 1125
Reply to #1:
You can use a regular expression rather than a simple string.

eg /A1|B2|C3/ will match A1, B2 or C3

The ".include" method only works with strings but you can use either ".test" or ".match" instead

".test" is applied to the regex with the string as an argument and returns true if the match is successful:

if (/A1|B2|C3/.test(item.name)) item.newPath = item.path + "Large Images";

Conversely ".match" is applied to the string with the regex as the argument and returns the result as an array of matches or NULL if the match is not successful:

if (item.name.match(/A1|B2|C3/)) item.newPath = item.path + "Large Images";

It's not clear what you are asking for in the second part of your question, but if, in your example, you want to move matching files into sub-folders A1, B2 and C3 you can simply save the result of each match and add it to the path...

if (pattern = item.name.match(/A1|B2|C3/)) item.newPath = item.path + "Large Images\\" + pattern;


22/02-23 19:56 - edited 22/02-23 19:59
#3 : 23/02-23 11:08
Emily
Emily
Posts: 8
Reply to #2:

Thank you very much for your help, that worked brilliantly!

Sorry, to clarify what I was asking in the last part of my question I meant that I need to move certain images into the "Selected" directory inside the "Large Images" directory.

When I saw the double backslash in the last part of your reply I added it in and then it worked for my sub directory too. Thank you! For that I used:

if (/A1|B2|C3/.test(item.name)) item.newPath = item.path + "Large Images\\Selected";

Many thanks again!


23/02-23 11:08
#4 : 23/02-23 12:57
David Lee
David Lee
Posts: 1125
Glad it helped!

Just to clarify the double-backslash:

The path separator is actually still a single backslash. However "\" is interpreted as the "escape" character, which modifies the meaning of the following character. Thus in a regular expression "." is interpreted as "match any character" but "escaping" - "\." - restores the original meaning, conversely "d" is interpreted as a lower-case letter d but, "when escaped", "\d" is interpreted as "match any decimal digit".

Thus for "\" to represent itself it must also be "escaped" - hence "\\".


23/02-23 12:57