Randomized file numbers possible?

Advanced Renamer forum
#1 : 29/05-19 23:58
Alex Foster
Alex Foster
Posts: 8
Is it possible to add file numbers in a randomized fashion? I have movie titles as a quiz but neither an alphabetical nor a chronological list will do because it would be too simple (and too boring).

I just want the program to assign numbers to my 40 or so files (from 01 to 40) but in a totally random order.

Is that possible?

Thanks in advance!


29/05-19 23:58 - edited 29/05-19 23:59
#2 : 30/05-19 00:30
David Lee
David Lee
Posts: 200
Use the Script method...

Pre batch script:

var nmax = 40;
var test = [0];
for (n = 0; n < nmax; n++) {
test[n] = 0;
}


Main script:

do {
x = Math.floor(Math.random() * nmax);
}
while (test[x] == 1);
test[x] = 1;
return ("0" + (x + 1)).slice(-2);


30/05-19 00:30
#3 : 30/05-19 11:54
Alex Foster
Alex Foster
Posts: 8
Reply to #2:

Wow... just wow.. I deeply admire your scripting knowledge. And thanks so much for the quick reply.

I applied the script and it produces random numbers as file names - but I still want the original file names, only preceded by randomly assigned numbers (but from a fixed range of numbers, of course). Can you help? So many thanks in advance!

To perhaps make it clearer: The 8 original files

The Black Cat.jpg
The Raven.jpg
Frankenstein.jpg
Bedlam.jpg
The Mummy.jpg
Targets.jpg
Tower of London.jpg
The Body Snatcher.jpg

should look like this:

05 The Black Cat.jpg
01 The Raven.jpg
08 Frankenstein.jpg
04 Bedlam.jpg
02 The Mummy.jpg
07 Targets.jpg
06 Tower of London.jpg
03 The Body Snatcher.jpg

since I have to insert the respective number into the screenshots to identify them in the quiz (my buddy looks at the screenshots and has to identify the movies by saying something like "Oh, 02 is easy, that's The Mummy").

--------------
Update 1: I found and tried the <Rand:MIN:MAX> tag which ALMOST works fine - if only the random numbers wouldn't occasionally repeat! (e.g. in a test batch of 8 files with <Rand:01:08> it produced three "7"s). Hm..... Any clues?

Update 2: I just applied your script to my 8 file test batch and changed the var nmax number from 40 to 8, which should (in my humble understanding of the matter) distribute all numbers from 1 to 8 in a random fashion to my 8 files... but still there were two "2s " and two "5"s (apart from the fact of course that the file names were gone --see above--).



30/05-19 11:54 - edited 30/05-19 12:56
#4 : 30/05-19 12:44
David Lee
David Lee
Posts: 200
Reply to #3:

Simple. "item.name" will return the original filename so just append it to the return value, with a suitable separator if desired:

return ("0" + (x + 1)).slice(-2)+ "-" + item.name;

If you type "item." into the script window a popup dialogue will show you all the available properties of the current item.

Random numbers are just that (well pseudo-random to be accurate) so each time a number is returned it can be any one of the available options - hence the random repetition.

In my script the variable "test" is an array that keeps track of which numbers have already been used. The pre batch script creates the variable and sets all its values to zero. Then in the main script, for each filename, random numbers are generated in a loop until one is found where the corresponding element of test is still zero (ie the number hasn't already been used) and the element of test is set to 1 to prevent the number being used again.

Math.random() returns a decimal number in the range 0.0000 ≤ n < 1.00000 and Math.floor(m) returns the integer part of the number m. So Math.floor(Math.random() * nmax) will return a random integer between zero and (nmax - 1).

The "Math" object isn't covered in Kim's brief introduction but you can find further information by searching online using the term "javascript" - I find help on the W3Schools particularly helpful.


30/05-19 12:44
#5 : 30/05-19 13:12
Alex Foster
Alex Foster
Posts: 8
Reply to #4:

Oh dear, I'm wasting your most precious time, so sorry for that. I was a little too late perhaps with my updated reply so I'll clarify it:

I would need the original file names, only preceded by randomly assigned numbers -but from a fixed range of numbers which is the total number of files.

So: The 8 original files

The Black Cat.jpg
The Raven.jpg
Frankenstein.jpg
Bedlam.jpg
The Mummy.jpg
Targets.jpg
Tower of London.jpg
The Body Snatcher.jpg

should look like this:

05 The Black Cat.jpg
01 The Raven.jpg
08 Frankenstein.jpg
04 Bedlam.jpg
02 The Mummy.jpg
07 Targets.jpg
06 Tower of London.jpg
03 The Body Snatcher.jpg

since I insert the respective numbers (01, 02, 03, 04, 05, 06, 07, 08) into the screenshots to identify them in the quiz (my buddy looks at the screenshots and has to identify the movies by saying something like "Oh, 02 is easy, that's The Mummy").

If you could tell me if that's possible I would be most grateful. Thank you so much for your time here.


30/05-19 13:12
#6 : 30/05-19 14:06
David Lee
David Lee
Posts: 200
Reply to #5:
Another simple edit - but this one requires an undocumented feature of Renamer:
"app.itemCount " will return the number of filenames in the list. So, in the pre batch script, set:

var nmax = app.itemCount;

Should be obvious - but to replace the "-" separator with a space:

return ("0" + (x + 1)).slice(-2)+ " " + item.name;

I don't understand why to you were seeing duplicate integers (Update 2 in your earlier comment) - it doesn't happen when I run the script.



30/05-19 14:06
#7 : 30/05-19 22:53
Alex Foster
Alex Foster
Posts: 8
Reply to #6:

So is this the finished script as it's supposed to be?

var nmax = app.itemCount;
var test = [0];
for (n = 0; n < nmax; n++) {
test[n] = 0;
}do {
x = Math.floor(Math.random() * nmax);
}
while (test[x] == 1);
test[x] = 1;
return ("0" + (x + 1)).slice(-2)+ " " + item.name;

I'm afraid to say it still returns double integers... I tried it with three files and it produces results like 02/01/02 or 03/01/01 - or indeed at one time it was 03/01/02 but that's coincidence, I guess.

Please, am I missing something here - or is anything wrong with the script as you see it here? Did I copy anything wrongly?


30/05-19 22:53
#8 : 31/05-19 00:18
David Lee
David Lee
Posts: 200
Reply to #7:
Yes - you appear to have seriously misunderstood the way that the Script method works.

These are supposed to be TWO SEPARATE scripts - not combined as one.

The first block of code (that initializes the test array to zero) should be executed once only before processing the filenames in the list and is entered as a "Pre batch script". Click the button at the top left of the main script window to open the separate Pre batch script window and click "Close and apply script" at the bottom right of the window when finished.

The second block of code is the batch script itself and is entered in the main script window. This code is then executed iteratively for every item in the list.

If you combine the code into a single batch script then all the elements of the test array will be reset to zero before each item is processed - so the script will not remember which integers have already been used.

The Pre batch script is only needed where code should be executed once only at the beginning of the run (such as for initializing variables) - for most scripts it will be left blank.

So...

In the Pre batch script window:

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

... and in the main script window:

do {
x = Math.floor(Math.random() * nmax);
}
while (test[x] == 1);
test[x] = 1;
return ("0" + (x + 1)).slice(-2)+ " " + item.name;

Hopefully this will now work as required.


31/05-19 00:18 - edited 31/05-19 00:19
#9 : 01/06-19 10:47
Alex Foster
Alex Foster
Posts: 8
Reply to #8:

Woohoo! You're a genius, thanks so very very much for your time and effort, it is most appreciated.

Alas... Can this script be combined with other methods? Apparently not (I tried but only either the New Name or Script method works, not both combined).

I want my files to ultimately look like this:

The Boris Karloff Quiz No. 05 The Black Cat.jpg
The Boris Karloff Quiz No. 01 The Raven.jpg
The Boris Karloff Quiz No. 08 Frankenstein.jpg
The Boris Karloff Quiz No. 04 Bedlam.jpg
The Boris Karloff Quiz No. 02 The Mummy.jpg
The Boris Karloff Quiz No. 07 Targets.jpg
The Boris Karloff Quiz No. 06 Tower of London.jpg
The Boris Karloff Quiz No. 03 The Body Snatcher.jpg

I was assuming methods can be combined so I didn't ask about that specifically.

But while I'm writing this I guess the answer is: Just apply the Script method and then load the altered files again and give them a new name part via the New Name method and voila...

-------
PS: To support you and this great project I just obtained a personal license. It was about time, I guess, after using the program for some time -- but your quick and easy willingness to spend time and brainpower on my little problem finally tipped the balance for me. Thank you so very much and best of luck!


01/06-19 10:47 - edited 01/06-19 11:18
#10 : 01/06-19 18:11
David Lee
David Lee
Posts: 200
Reply to #9:
Try the Add method in the same batch - following the script...

Add: "The Boris Karloff Quiz No. "
At index: 1

However you can just add this prefix to the return value of the script.

To make the script easily modifiable put:
var prefix = "The Boris Karloff Quiz No. "
at the beginning of either the script or pre batch script

and modify the return statement to:
return prefix +("0" + (x + 1)).slice(-2)+ " " + item.name;

BTW - I'm just a user - this excellent application is the work of Kim Jensen, who I am sure will be grateful for your support. I find trying to solve other folks problems is a great way of improving my Javascript and Regex skills for the times that I really need them in a hurry!


01/06-19 18:11
#11 : 02/06-19 10:36
Alex Foster
Alex Foster
Posts: 8
Reply to #10:

Again, works like a charm!

Reg. your authorship: Oops! :-) Anyway, such an great piece of software is really worth paying for, I guess.

Thank you deeply once again and good luck for you and your Java skills - believe me, they're excellent as they are already.

CU!


02/06-19 10:36
#12 : 13/06-19 09:12
John Wick
John Wick
Posts: 1
Can i used advanced renamer for my website. Actually i running comic books site i want to manage my chapters in a sequence, So i thought to make a plugin of advanced renamer and publish it on wordpress.

You can check the chapters sequence that i manually do on daily basis like this: https://www.mangazuki.online/manga/quanzhi-fashi


13/06-19 09:12 - edited 13/06-19 09:13