Use with items count

Advanced Renamer forum
#1 : 19/10-21 15:56
Louis Routhier
Louis Routhier
Posts: 3
Hi,
I want to randomly number a bunch of files. I have seen around many people mentioning the <rand:min:max> tag which works good in an interactive way but I would like to use it in a command line and since I have to manually modify the max to my items count each time, I was wondering if there is a way of doing something like this: <rand:1:<Num Items>>

(I have seen some posts mentioning pre-batch scripts but I would prefer to not go there)

Thanks :)


19/10-21 15:56
#2 : 19/10-21 18:06
David Lee
David Lee
Posts: 797
Easy to do in a script:

newName = app.parseTags("<Rand:1:" + app.itemCount + ">");
return newName;

However in order to ensure that each number is only used once (ie to avoid duplicate filenames) you will have to keep track of which numbers have been used and keep generating random numbers until you obtain a new one.

For this you will need to create an array in a Pre batch script....

Pre batch script:

var test = [0];
max = app.itemCount;
for (n = 1; n < max + 1; n++) {
test[n] = 0;
}

Main script:

do {
x = Math.floor(Math.random() *max) +1;
}
while (test[x] == 1);
test[x] = 1;
return x;



19/10-21 18:06 - edited 19/10-21 18:27
#3 : 22/10-21 06:23
Louis Routhier
Louis Routhier
Posts: 3
Reply to #2:
Nice! Thanks! The use of the bitflag array is quite interesting. Probably faster than using a list an removing items from it and reducing count. I never used script before. It is quite powerful!


22/10-21 06:23