Rename files with random alphanumeric characters

I'm wanting to write a script to rename files with 6 uppercase alphanumeric characters.

Example:

001.jpg
002.jpg
003.jpg

KT7S89.jpg
ZTY1S5.jpg
U2L7HP.jpg

By adapting other scripts I got the following code, but it doesn't work, could you help me once more?

function OnClick(clickData) {

   var cadenaAleatoria {
       var caracteres = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
       var aleatoria = "";
       for (var i = 0; i < 6; i++) {
 	      var aleatoria = caracteres(Math.floor(Math.random() * caracteres.length));
       }
   }

   var cmd = DOpus.Create().Command;
   cmd.RunCommand('Rename IGNOREEXT PATTERN * TO aleatoria');
}

var aleatoria = caracteres(Math.floor(Math.random() * caracteres.length));
Change to
aleatoria += caracteres[Math.floor(Math.random() * caracteres.length)];

And variable should be outside quotes.
cmd.RunCommand('Rename IGNOREEXT PATTERN * TO ' + aleatoria);

function OnClick(clickData) {

    var caracteres = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    var aleatoria = "";
    for (var i = 0; i < 6; i++) {
 	     aleatoria += caracteres[Math.floor(Math.random() * caracteres.length)];
    }

    var cmd = clickData.func.Command;
    cmd.RunCommand('Rename IGNOREEXT PATTERN * TO ' + aleatoria);
}
1 Like

Oh @WKen! You save my life again, I don't know how to thank you so much, thank you very, very much, but I wanted to ask you one thing, if I rename the photos one by one everything is perfect, but if I select several, after renaming the first one a message is generated error "Cannot create a file that already exists." How could I resolve that situation?

2023-08-27 08 52 20

?
If you rename multiple files of the same type you need to check if the file already exists.

Unfortunately I don't know how to check if a file already exists

Piece these together.

    var sourcetab = clickData.func.sourcetab;
	var sourcepath = sourcetab.path;
	var cmd = clickData.func.Command;
	var fsu = DOpus.FSUtil;
	for (var e = new Enumerator(sourcetab.files); !e.atEnd(); e.moveNext() )
	{
	    eItem = e.item();
	}
    cmd.Run();
	    while (fsu.Exists(newFile)) {
		      aleatoria = randChars();
			  newFile = sourcepath + "\\" + aleatoria + eItem.ext;
		}
function randChars() {
    var caracteres = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    var aleatoria = "";
    for (var i = 0; i < 6; i++) {
 	     aleatoria += caracteres[Math.floor(Math.random() * caracteres.length)];
    }
	return aleatoria
}

@WKen take a look here, please, to see if I'm doing something wrong because it's giving an error on the line while (fsu.Exists(newFile)) { newFile is not defined?

function OnClick(clickData) {
    var sourcetab = clickData.func.sourcetab;
	var sourcepath = sourcetab.path;
	var cmd = clickData.func.Command;
	var fsu = DOpus.FSUtil;
	for (var e = new Enumerator(sourcetab.files); !e.atEnd(); e.moveNext() )
	{
	    eItem = e.item();
	}
	    while (fsu.Exists(newFile)) {
		      aleatoria = randChars();
			  newFile = sourcepath + "\\" + aleatoria + eItem.ext;
		}
	function randChars() {
    var caracteres = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    var aleatoria = "";
    for (var i = 0; i < 6; i++) {
 	     aleatoria += caracteres[Math.floor(Math.random() * caracteres.length)];
    }
    cmd.Run();
	return aleatoria
  }
}

I misplaced it. :sweat_smile: cmd.Run();
You didn't define newFile first.
And put it into a for loop: cmd.AddLine('Rename IGNOREEXT "' + eItem + '" TO ' + aleatoria);

I saw other problem in your script, giving you my full code. . .

function OnClick(clickData) {

    var sourcetab = clickData.func.sourcetab;
	var sourcepath = sourcetab.path;
	var cmd = clickData.func.Command;
	var fsu = DOpus.FSUtil;
	for (var e = new Enumerator(sourcetab.files); !e.atEnd(); e.moveNext() )
	{
	    eItem = e.item();
		aleatoria = randChars();
		var newFile = sourcepath + "\\" + aleatoria + eItem.ext;
	    while (fsu.Exists(newFile)) {
		      aleatoria = randChars();
			  newFile = sourcepath + "\\" + aleatoria + eItem.ext;
		}
        cmd.AddLine('Rename IGNOREEXT "' + eItem + '" TO ' + aleatoria);
	}
	cmd.Run();
}

function randChars() {
    var caracteres = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    var aleatoria = "";
    for (var i = 0; i < 6; i++) {
 	     aleatoria += caracteres[Math.floor(Math.random() * caracteres.length)];
    }
	return aleatoria
}

I don't want to tire you, but now several files are renamed well, but even those not selected are renamed, sorry

Change the object, view the tab object, sourcetab.files, I can't open the online Opus Manual. :no_mouth:

And add:

if (sourcetab.selected_files.count == 0)
    return

If you're renaming things, you're probably better off using a rename script. Then you only have to worry about generating the filenames you want, and not everything else like which files to work on.

https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/Rename_Scripts1.htm

1 Like

True as always Leo, thanks, I'll try to migrate this code there

Thank you very much @Leo again, thanks to your brilliant idea we made it very simple. Thank you very much too @WKen for the hard and constant work, I don't know how to thank you so much!

In case the code interests anyone, it is to be used from the Advanced Renaming window.

function OnGetNewName(getNewNameData) {
    var item = getNewNameData.item;

    var caracteres = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    var random = "";
    for (var i = 0; i < 6; i++) {
 	     random += caracteres[Math.floor(Math.random() * caracteres.length)];
    }

   var name= random + item.ext;
   return name;
}
1 Like

works great, many thanks.

1 Like

Thank you!

To run directly from a toolbar button...

Note: In the "Function" field we must select the option "Standard function (Opus or external)", not "Script function", even if it really is a script.

Rename PATTERN * TO *
@script jscript

function OnGetNewName(getNewNameData) {
    var item = getNewNameData.item;

    var caracteres = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    var random = "";
    for (var i = 0; i < 6; i++) {
 	     random += caracteres[Math.floor(Math.random() * caracteres.length)];
    }

   var name= random + item.ext;
   return name;
}

To include lowercase letters:
var caracteres = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

To include only numbers:
var caracteres = 0123456789';

To change the number of characters, for example, from 6 to 12, change:
for (var i = 0; i < 6; i++) { for for (var i = 0; i < 12; i++) {

You can also save the script into a Rename Preset and run that from a button (or from the menu on the right of the standard Rename button, which lists all your rename presets automatically).

That way you can use the same script inside the Rename dialog and outside it, without defining everything twice.

Maybe the code should not be marked as JS, because it needs to use standard function. May be confusing to new users. . .

Very true Leo, it is another option that we have available, much more versatile and intelligent, thank you very much!