Renumbering uniquely-named files starting at 1

Advanced Renamer forum
#1 : 03/06-21 08:04
Peter
Peter
Posts: 1
Is it possible to renumber each uniquely-named file starting at 1?

I used the renumber method as shown here: https://ibb.co/RvWK9Rq

"aris" starts at 1, I would like "ki" and "zu" to start at 1 as well, if possible.


03/06-21 08:04
#2 : 03/06-21 10:31
David Lee
David Lee
Posts: 717
You will need to use a Script method to keep track of the individual filenames.

The following code will work for your dataset (but is not robust):

name = item.name.match(/.*_(?=\d+$)/)[0];
if(!names[name]) names[name] = 0;
return name + ("0" + ++names[name]).slice(-2);

The first line uses a regular expression to extract the filename up to and including the underscore.
We then use an object "names" to keep track of the number of occurrences of each "name" - this works like an array, but uses "name" as an index instead of a number.
Line 2 checks whether the object element "name" has been defined and if not creates it and sets its value to zero.
Line 3 adds 1 to the saved number and adds it to the beginning of the filename, zero-packed to 2 digits.

Whilst this will work for the data you posted, it may fail if any of the new filenames existed in the original list of names - in which case ARen will detect name collisions when it tries to apply the new names if the filenames already exist. You can see the effect of this if you take out the zero-packing.

ie replace the last line of the script with:

return name + ++names[name];

To get around this problem you will have to use two passes to rename the files.
First rename as above but adding a temporary character to the beginning of each filename:

name = item.name.match(/.*_(?=\d+$)/)[0];
if(!names[name]) names[name] = 0;
return "#" + name + ("0" + ++names[name]).slice(-2);

Run this batch and reload all your files into the list - then remove the temporary character from each filename in a second batch, using a Replace method:

Replace ^#
With: leave blank
Use regular expressions


03/06-21 10:31 - edited 06/06-21 11:33