Randomize name in a comma-separated

Advanced Renamer forum
#1 : 02/07-21 13:57
alice nguyen
Posts: 1
Hello. I have many files like this:

fondant, jar, dessert, cubes, food.jpg
grapes, strawberries, fruits, water, food.jpg

How to randomly change the location of words that being separated by comma in the file name. I want to rename the files like this:

fondant, cubes, dessert, food, jar.jpg
strawberries, food, fruits, grapes, water.jpg

Thank you.
#2 : 02/07-21 15:34
David Lee
Posts: 1125
You will have to use a script method.

Split the filename into an array of individual words then randomize the array using the Fisher-Yates method...

words = item.name.split(", ");
for (j = words.length -1; j>0; j--) {
k = Math.floor(Math.random() * j)
temp = words[j]
words[j] = words[k]
words[k] = temp
}
name = words[0];
for (j=1; j<words.length; j++) name += (", " + words[j]);
return name;


or without the temp variable (slightly more obscure!)...

words = item.name.split(", ");
for (j = words.length -1; j>0; j--) {
k = Math.floor(Math.random() * j)
words[j] = [words[k], words[k] = words[j]][0];
}
name = words[0];
for (j=1; j<words.length; j++) name += (", " + words[j]);
return name;

edited: 02/07-21 15:45