How to Make Specific Name Change

Advanced Renamer forum
#1 : 30/05-21 03:38
Cory Kent
Cory Kent
Posts: 2
Hello everyone. I'm trying to perform a specific name change on some files, however I have a brain injury so my learning curve is very steep. I'm unable to determine from the various methods described in the user guide, nor can I find an answer for this in the forum, and this is why I'm posting a question.

I'd like to rename some files to numbers. This is easy enough, however I get errors and have problems when I end up with many files with the same number names. I'd like to set the program to change the files to say a bunch of 1s or 0s, randomly, but the methods I know to do that. Don't let me change files completely sometimes, if changing a given number or letter to a 1 or 0 means that the new file will be the same.

Is there a way to say, change all the number 2s to a random sequence of 1s or 0s, making sure that none end up the same, so they can all be accepted by the program? Does this make sense? Because of my brain problem I don't know how I sound or if I am explaining myself properly.

Just to be clear, let's say I have files such as this:

122345
122346
122347

I can change the sequence 12234 to any combination of 111000101010101 or whatever.

But then when I go to change 5 to some combination, say 101, I have do something else for the 6, and it has to be different. This is fine if you see 3 like that, but not if I have 15 different files with a 2 left at the end, 20 with a 3, 10 with a 4, and so on. Then I'd have to do each file one by one with that method, and in that case I may as well do them all individually in the first place.

Any help would be very appreciated. Thank you most kindly.


30/05-21 03:38 - edited 30/05-21 03:39
#2 : 30/05-21 13:23
David Lee
David Lee
Posts: 1125
It's difficult to understand what you are after - but it seems like all you want to do is simply to rename each file with a non-repeating random binary number.

It is quite straightforward to do this using a script. Basically, for each filename, keep generating random integer decimal numbers until you obtain one that hasn't already been used and then convert this to binary to rename the file. Try this code...

Create a Script method

Click "Pre batch script" and enter the code:

var used = [];

Click "Close and apply script" and enter the following code in the main script window:

digits = 16;
do n = Math.floor(Math.random() * 2**digits);
while (used[n]);
used[n] = 1;
bin = n.toString(2);
return (('0'.repeat(digits)) + bin).slice(-digits);

Explanation....

"var used = [];" (Pre batch script)
First we define an empty array, "used", that we will use in the main script to keep track of the numbers already allocated to filenames. (Note that code in the Pre batch script is run once only, before renaming commences).

Main Script:
"digits = 16;"
Specify the number of binary digits (1 & 0) for each filename.

"do n = Math.floor(Math.random() * 2**digits);
while (used[n]);"
This generates a random integer, n, in the range from zero to the maximum possible number for the specified number of binary digits (inclusive). The statement is in a loop which keeps repeating until it returns an unused number.
"Math.random()" generates a random decimal number less than 1.0
Multiply this by "2**digits" (ie 2 raised to the power of digits), which is one greater than the decimal value of the maximum binary number.
"Math.floor()" rounds the number down to the nearest integer.

"used[n]" returns the value saved in element "n" of the array: "used". If the element does not yet exist this will return the value "undefined", which will be interpreted as "false" by the "While" statement and the program will jump out of the loop, otherwise the loop will repeat choosing a new random number. Once an unused value of "n" has been obtained the array element: "used[n]" will then be created and set to "1".

bin = n.toString(2);
This converts the numerical variable "n" to a string representation, specifying base 2 (ie binary)

return (('0'.repeat(digits)) + bin).slice(-digits);
Finally the binary number is returned as the new filename, using a trick to add zero-padding as appropriate. (Trick is to add a string of zeros to the beginning of the name and then slice off the desired number of characters from the end).



30/05-21 13:23 - edited 30/05-21 16:56
#3 : 30/05-21 22:49
Cory Kent
Cory Kent
Posts: 2
Reply to #2:
First of all, thank you for your reply. Yes with my brain injury it's hard for me to explain myself sometimes, especially if I don't know the words for things, like a binary number.

But you were correct, what you thought I was needing to do from my post as I wrote it. I was trying to write over my filenames with random 1s and 0s, but of course non repeating.

Your script worked perfectly. I had trouble at first for some reason, but when I went in an redid it, changing 16 digits to 21, it worked exactly as it should. And there's no way I could have understood what to write in the script myself.

I'd already thought about the script option, but have no idea how the language works for scripts, either in this software or elsewhere in programs.

Thank you so much!


30/05-21 22:49