Name collision not ignoring extension

Advanced Renamer forum
#1 : 05/06-20 09:21
Mohammad Ibrahim Shaikh
Posts: 3
Can I make name collision ignore the extensions? I will explain with simple example
What I am getting is

Apple_001.jpeg
Apple_002.jpeg
Apple_001.mp4
Apple_002.Mp4
Mango_001.jpeg
Mango_002.jpeg
Mango_001.mp4
Mango_002.Mp4

What I Want is

Apple_001.jpeg
Apple_002.jpeg
Apple_003.mp4
Apple_004.Mp4
Mango_001.jpeg
Mango_002.jpeg
Mango_003.mp4
Mango_004.Mp4

I cannot use <Inc> because I have different Prefixes
#2 : 05/06-20 10:53
David Lee
Posts: 1125
No. The collision rules are only applied when there are resulting duplicates of filenames - including extensions. However you can achieve the same result using some advanced scripting.

I'm assuming that your existing methods are resulting in New Filenames, ordered as:

Apple.jpeg
Apple.jpeg
Apple.mp4
Apple.Mp4
Mango.jpeg
Mango.jpeg
Mango.mp4
Mango.Mp4

What we need to do is to iterate through this file list setting the suffix to '_001" each time we encounter a new name and incrementing the suffix if the name is the same as the previous file.

You can do this in the Pre batch script of the Script method.

Add a script method at the end of your Method list.
First delete the return statement in the main script window.
Then use the following code in the Pre batch script:

noFiles = app.itemCount;
lastName = '';
for (j = 0; j < noFiles; ++j) {
item = app.getItem(j);
name = item.newBasename;
if (name != lastName) {
inc = 1;
lastName = name;
} else {
++inc;
}
suffix = ('00' + inc).slice(-3);
item.newName = name + '_' + suffix + item.ext;
}

Note that '' in the second line (lastName = '';) is two single quotes NOT a double quotation mark.


edited: 05/06-20 10:55
#3 : 06/06-20 14:00
Mohammad Ibrahim Shaikh
Posts: 3
Reply to #2:
Thanks a lot, it worked like magic though I didn't understand a single line of code.
Thank you so very much, It has made my life a lot easier.
#4 : 06/06-20 19:26
Mohammad Ibrahim Shaikh
Posts: 3
Reply to #2:
Just one last thing. if I have to zero pad 4 zeros what should I change, I am doing this but it is not working, I don't know what I am doing wrong

noFiles = app.itemCount;
lastName = '';
for (j = 0; j < noFiles; +++j) {
item = app.getItem(j);
name = item.newBasename;
if (name != lastName) {
inc = 1;
lastName = name;
} else {
+++inc;
}
suffix = ('000' + inc).slice(-4);
item.newName = name + '_' + suffix + item.ext;
}
#5 : 06/06-20 20:43
David Lee
Posts: 1125
Reply to #4:
That's what you need to do so I'm surprised that it's not working. What errors are you seeing?

edited: 07/06-20 14:12
#6 : 07/06-20 14:30
David Lee
Posts: 1125
Reply to #5:
Just spotted your problem.

You have changed the increment operator in lines 3 and 10 from "++" to "+++", which is a meaningless operator and so will throw an "Invalid script" error.

++j means increment the value of j by one.

ie same result as j = j + 1