Replacing last file of a folder a certain way.

Advanced Renamer forum
#1 : 17/01-19 04:32
Posts: 50
Currently I have many many folders with image files that I want named in a particular format:

img_001.jpg = _Bali_Lau-Ma-Restaurant_cover1.jpg
img_002.jpg = Indonesia_Lau-Ma-Restaurant_Bali_002.jpg
img_003.jpg = Indonesia_Lau-Ma-Restaurant_Bali_003.jpg
img_004.jpg = Indonesia_Lau-Ma-Restaurant_Bali_004.jpg
img_005.jpg = _Bali_Lau-Ma-Restaurant_cover2.jpg

I get the <Country>_<Venue>_<City> info from the directory the files are in, so don't worry about that.

As in the example, the first and last file of each folder are the cover images that I want renamed in a different way. The first file is easy to rename because it is always img_001.jpg. But the last file is a challenge because each folder has different number of files, so the last file will be differently numbered in each folder. So currently I have to load each folder separately and edit the Replace Rule manually so that the last file is renamed as a cover file.

Is there a way to create a rule that dynamically picks the file with the max number in a directory and rename it in cover format above? This way I can load all the folders at the same time and rename them all in one go, since the format is the same for them all.

17/01-19 04:32 - edited 17/01-19 04:35
#2 : 17/01-19 12:24
David Lee
David Lee
Posts: 245
Reply to #1:
If you use a script, item.filesInDir returns what it says.

17/01-19 12:24 - edited 17/01-19 12:47
#3 : 17/01-19 14:54
Posts: 50
Reply to #2:

Thanks for the reply. I have no clue how to script, though. I'll give it a shot.

17/01-19 14:54
#4 : 17/01-19 17:34
David Lee
David Lee
Posts: 245
Reply to #3:
Neither did I when I started with ARen!

To get started see

For more info on syntax try Googling for JavaScript commands - I found hits at particularly helpful.

17/01-19 17:34
#5 : 18/01-19 15:21
David Lee
David Lee
Posts: 245
Reply to #4:

The script below will do what you want:

img_001.jpg => img_cover1.jpg
img_002.jpg => img_002.jpg
img_003.jpg => img_003.jpg
img_004.jpg => img_004.jpg
img_005.jpg => img_cover2.jpg

Note that you must insert the script method at the beginning of the batch, before your other methods (or in a separate pass), as "" returns the ORIGINAL filename.

x =;
txt = x.slice(0,-3);
num = Number(x.slice(-3));
if (num == 1) {
x = txt + "cover1"
} else if (num == item.filesInDir) {
x = txt + "cover2"
return x;

18/01-19 15:21 - edited 18/01-19 16:48
#6 : 21/01-19 15:10
Posts: 50
Reply to #4:

**reply deleted by me**

Nevermind, did not see your reply. I will check your script out.

21/01-19 15:10 - edited 21/01-19 15:14