Randomize name in a comma-separated
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.
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.
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;
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;