Command: SelectRandom (randomly select files/folders)

SelectRandom - a script command which allows you to randomly select items by type, count, size, total-size or some other filters. Results can be selected, dumped to console, copied to file collections or returned in env/tab-variables.


"SelectRandom" is obsolete and won't receive any updates.
The functionality of this command has been moved into "SelectEx". Find it here:

Read this quick-tip on how to update an existing button:


Why:
I made this..

  • to create a random selection of mp3 albums to listen to
  • to select random albums/images to view in a slideshow
  • because I have fun doing it.. o)

Params and Features:
FOLDERS - select folders only (default is files or folders)
FILES - select files only (default is files or folders)
NODESELECT - do not deselect items (useful for keeping the selection or adding single items each run)
RANDOM - select a random number of items
SINGLEFOLDER - select a single folder if there are at least SINGLEFOLDERMINCOUNT numbers of folders
SINGLEFOLDERMINCOUNT - number of folders, used in conjunction with SINGLEFOLDER switch
ITEMCOUNT - how many items to select (default is 1)
ITEMCOUNTPERC - how many items to select in percent
ACTION - optional action to perform ("dblclk" eg., lookup FileType ACTION command and options)
COMMANDS - raw commands to run on selection, separate by "\n", make sure param is last
PATH - use external path instead of tab, meant to be used with SETTAB/ENVVAR (items will not be visually selected)
SETTABVAR - the tab scoped variable name, containing chosen items (full paths), separated by ";"
SETENVVAR - the process scoped env-variable name, containing chosen items (full paths), separated by ";"
NAME" - filter items by name/extension (regular expression)
RECURSE - recursive mode, meant to be used with PATH
JSFILTER - for custom scripting filter logic
MINSIZE - to filter files by minimum file size
MAXSIZE - to filter files by maximum file size
MAXSIZETOTAL - to limit total size
COPYTOCOLL - copy results to a file collection
ECHO - output results to the console for easier testing

If you have trouble using it, the script config section offers a trigger to enable some console output, if that does not help, just ask! o)

Hint:
For selecting a single item out of many, it is recommended to run "ScrollSelectionIntoView" after running "SelectRandom", to scroll the selected item into the visible area. Find it right here:

Examples - Usecases:
Select 10 random music albums folders and add these to winamp:

SelectRandom FOLDERS ITEMCOUNT=10 COMMANDS=WinampAddItems

Select a random file, run the doubleclick action and make item visible (chooses a movie and autom. launches player eg.):

SelectRandom FILES ACTION=dblclck COMMANDS=ScrollSelectionIntoView

Select 20 images and start slideshow:

SelectRandom FILES ITEMCOUNT=20 COMMANDS=Show SLIDESHOW

Select 1 file out of an "external" folder and provide result in an env- and tab-var called "Result.SelectRandom":

SelectRandom FILES PATH="C:\Items" ITEMCOUNT=1 SETTABVAR="Result.SelectRandom" SETENVVAR="Result.SelectRandom"

Examples - some more:

Select a random item:

SelectRandom

Select 10 random items:

SelectRandom ITEMCOUNT=10

Select 10 random folders:

SelectRandom FOLDERS ITEMCOUNT=10

Select 50% of files:

SelectRandom FILES ITEMCOUNTPERC=50

Select a random number of random items:

SelectRandom RANDOM

Select 1-5 random items:

SelectRandom RANDOM ITEMCOUNT=5

Select a single file, or a single folder if there are at least 3 subfolders present.

This creates a button that when repeatedly pressed, opens random folders and eventually "doubleclicks" a file

SelectRandom FILES SINGLEFOLDER ACTION=dblclk

Another Demo:
Select 50% of items



Installation:
To install the command, download the *.js.txt file below and drag it to Preferences / Toolbars / Scripts.
After that the command shall be ready to use in buttons, scripts and externally from dopusrt.exe of course.

cya,
tbone

Download:

1 Like

This is awesome. Thanks for sharing, tbone!
:thumbsup:

I thank you! o) Do you have a usecase already? I'm curious on other peoples ideas in conjunction with random selections. In case there's something I can add, let me know as well. o)

Mainly for random music. Great to go into flat view, hit a button to select random songs to listen to.

My second case would be for selecting a random thumbnail for a folder. Sometimes it's really hard picking from hundreds of images for a really good thumbnail to represent a folder. Now I can click a button and let it randomly pick for me. If I don't like it, I can click again :slight_smile: Making a copy of the selected image, naming it folder.jpg and hiding it, is easy. Might need a third party program to auto crop the image to a 1:1 aspect ratio. Not sure Dopus can do the cropping to square. Something I figure out later.

I haven't had time to fully sit down and code the buttons I want so not sure if it's missing anything. The only thing I was hoping for was an "About" screen that showed a click-able link to this post so I could look up your example buttons later without bookmarking it. Seems like a nice way to always link back to your original post :slight_smile:

I've been waiting for a random select script for a long time now. So far I've only tested with your example buttons and it seems to work perfectly. I'll figure out what I really want my buttons to be when I have more time.

Thanks again for creating and sharing!

1 Like

Love this script, it is really useful when I don't have an idea what to watch, read or listen to.

I would like to suggest adding extension-filtering (useful for excluding .txt, .db, .exe, .nfo, .js, .vbs and so on), and
a MAKEVISIBLE parameter. The latter should simply add " MAKEVISIBLE" to the select command.

I don't think I'm alone having thumbs.db, batchfiles, etc somewhere in the flat view.
Then there are those extensions that isn't fun executing by accident when using this command (tied to a hotkey):

SELECTRANDOM FILES ACTION=dblclk

I agree, filtering for extensions is something I also had in mind. While it's not availabe within this command, you probably can get around by setting a show filter in the same button right before SelectRandom runs? I did not try that though, places where I use SelectRandom are kind of "unmixed". o) Will see, if I can add that nonetheless.

Nice script, very useful for random media-playlists.

I have two other ideas:

  • Select max. x MB (e.g. 4,5 GB for fitting on DVD)
  • Select music-tags (like genre and year)

I would do myself if my scripting skills would be good enough. :cry:

I understand the wish for a random, but size-limited selection, but to get a random selection based on genres, years or something else, some heavier interaction with a GUI is required I guess, so what about using the Find-Utility to get a list of items (by searching for genre x,y,z and year 1,2,3 et.c) and then run SelectRandom on that find result? That sounds like it should do the trick, what do you think?

Yes, good idea!

Updated to v0.2:

  • new option "PATH", to choose items from any folder
  • new option "SETENVVAR", the given env-var will contain item-paths
  • new option "SETTABVAR", the given tab-var will contain item-paths

The new options allow to "select" from an external folder in which case the results can be put into variables to be used by scripts or buttons in different ways. The random item selection is not done visually in the filedisplay in this case.

This snippet demonstrates how to choose a random item from C:\Windows and print its name into the script console.

var cmd = data.func.command;
cmd.RunCommand('SelectRandom PATH="C:\\Windows" ITEMCOUNT=1 SETTABVAR="Tab.SRResult" SETENVVAR="Env.SRResult"');

DOpus.Output("TabResult: " + data.func.sourcetab.Vars.Get("Tab.SRResult"));
DOpus.Output("EnvResult: " + new ActiveXObject("WScript.Shell").Environment("Process")("Env.SRResult"));

Updated to v0.3:

  • new option "NAME" to filter items by name/extension (regular expression)
  • new option "RECURSE" recursive mode, meant to be used with PATH
  • new option "JSFILTER" for custom scripting filter logic
  • new option "MINSIZE" to filter files by minimum file size
  • new option "MAXSIZE" to filter files by maximum file size
  • new option "MAXSIZETOTAL" to limit on total size (ITEMCOUNT precedes)
  • new option "COPYTOCOLL" to copy results to a file collection
  • new switch "ECHO" to output results to the console for easier testing

You can now filter what get's selected/chosen by name/extension (NAME option) and item size (with MIN/MAXSIZE).
To select 8GB of files, use the new MAXSZIZETOTAL option. All size-related options support friendly sizes like "1mb" or "0.5gb" etc. Foldersizes will be taken into account and determined automatically if required.

There's also an option to pass a snippet of js-code to allow for custom filtering. You can create queries that randomly select files by mp3-genre e.g. This example fetches 10 random "NewAge" tracks from D:\music and stores them in a new collection called "NewAge" (notice: using the RECURSE switch takes some time for heavier music collections).

var jsFilter = ""+
    "if (item.metadata == 'audio'){ "+
        "var genre = new String(item.metadata.audio['mp3genre']);"+
        "if (genre.indexOf('New Age')!=-1) return false; /*do not filter*/"+
    "}";

cmd.RunCommand('SelectRandom ITEMCOUNT=10 PATH="D:\\music" RECURSE JSFILTER="'+jsFilter+'" COPYTOCOLL=NewAge');

A major cleanup has been done, if you like an example on how to defrag your code, take a look. I think it's quite nice! o)

"SelectRandom" is obsolete and won't receive any updates.
The functionality of this command has been moved into "SelectEx". Find it here: Command: SelectEx (extended Select command)

Any SelectRandom call/button can be updated to work with SelectEx by turning:

SelectRandom <your options here>

into

SelectEx RANDOM <your same options here>

Thanks! o)

Is there someone who knows how to use the copyToColl. I past the full path of the folder where the selected files should be moved but I get an error. Any help could be appreciated.

Details of what you tried and what the error message said would help us tell you what was wrong.

Thank you. This is the way I try to refer to a folder with copyToColl. Is it the correct way?
copyToColl : GetParameter(data, "COPYTOCOLL", null, "C:\Users\adam\OneDrive\Desktop\Assets\Test"),

Where are you entering that? That looks like script code, but if you're using it as a way to run the SelectRandom command in a normal (non-script) button or hotkey then it doesn't look right to me.

Doesn't look like any of the examples in the root post either. If you're just trying to run the command, it should look like those examples.

You're also not specifying the collection to copy to. I'm not sure what you're aiming to do exactly.

SelectRandom COPYTOCOLL="My Collection" might work. Or it might need
SelectRandom COPYTOCOLL="coll://My Collection"

It'll probably need some other arguments as well, depending on what you want to do, but I don't know what you want to do (and also haven't used the SelectRandom script/command before.)

I would like to give an example what I'm trying to do here. I have a folder named "slideshow". When I open the folder "Pictures" where I store saved images. I press the buttoon SelectRandom and the script selects 5 random images. Simultaneously, I would like the selected 5 images to be moved to the existing folder named "slideshow". Should I past the full path of the folder "slideshow" using the option COPYTOCOLL? Or should I do something in a different way?

This doesn't sound like it has anything to do with File Collections, which is what the CopyToColl argument is for.

File Collections store lists of files from different places, like you get if you do a search across multiple folders. If you just want to copy some files from one place to another, collections probably aren't going to be involved. But maybe I have misunderstood what you're trying to do.

I believe I just didn't know what File Collections implied :grinning:. Yes, you understood what I'm trying to do. Just want to move the selected random images to a folder. Maybe, the author tbone will respond

Please, can you guys help me to select only files

files : GetParameter(data, "FILES", null, null),

What shoud I write here in order to select only files and not folders? I tried different things here but didn't work.