Rename files with random names

Javascript renaming examples. Javascript renaming is supported in version 3 or newer.

Rename files with random names

Postby Admin » Wed Oct 19, 2016 3:30 am

Is there a way to rename a group of files with random names (any kind of alpha numeric string)?
Admin
Site Admin
 
Posts: 1467
Joined: Tue Mar 08, 2005 8:39 pm

Re: Random Names

Postby Admin » Wed Oct 19, 2016 3:32 am

Using the Javascript functionality in Special (14) you can assign random names.

Code: Select all
function makeid(size)
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < size; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}
newName = makeid(15);


Note that 15 in makeid(15) is the required name size.
Admin
Site Admin
 
Posts: 1467
Joined: Tue Mar 08, 2005 8:39 pm

Re: Rename files with random names

Postby Ardwych » Tue Jan 31, 2017 7:27 am

Here's a variation that appends a random set of three digits to existing filenames*, to photos, for instance.

Code: Select all
function makeid(size)
{
    var text = "";
    var possible = "0123456789";

    for( var i=0; i < size; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}
newName = name + '_' + makeid(3);

If you change this code yourself, note that spaces before and after elements like name, +, '_', are required. Omitting them throws up errors.

NOTE that after you paste the above text into the javascript panel**, the Test button lets you check the code by changing the name of test_file_name.ext You can experiment with the code and re-test. The files themselves don't appear to get saved.

*if you would like to append four digits rather than three, change makeid(3) in the bottom line to makeid(4), ad infinitum..
**save that setup/configuration by choosing File, Save As - to retain it as a .bru Favourite file (otherwise the next time you open the javascript panel, it will be blank).
Ardwych
 
Posts: 9
Joined: Mon Jan 21, 2008 2:24 pm

Re: Rename files with random names

Postby Admin » Wed Feb 01, 2017 12:03 am

Good one! :)
Admin
Site Admin
 
Posts: 1467
Joined: Tue Mar 08, 2005 8:39 pm


Return to Javascript Renaming