# Command: SelectRandom (randomly select files/folders)

**URL:** <https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289>\
**Category:** Buttons/Scripts\
**Tags:** script, commands, jscript\
**Created:** [September 28, 2014, 8:12am UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289 "2014-09-28T08:12:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbone](https://resource.dopus.com/user_avatar/resource.dopus.com/tbone/32/25726_2.png) [@tbone](https://resource.dopus.com/u/tbone)\
**Post date:** [September 28, 2014, 8:12am UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/1 "2014-09-28T08:12:28Z")

</div>

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:**

> [@Command: SelectEx (extended Select command)](https://resource.dopus.com/t/command-selectex-extended-select-command/20517):
>
> SelectEx - a script-addin for extended "Select" functionality. It tries to combine and enhance on some (unmaintained) contributions. This add-in allows you to remove several standalone scripts from your configuration and tries to be as versatile as t…

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

> [@Command: SelectRandom (randomly select files/folders)](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/12):
>
> "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)](https://resource.dopus.com/t/command-selectex-extended-select-command/20517/1) Any SelectRandom call/button can be updated to work with SelectEx …

* * *

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:

> [@Command: ScrollSelectionIntoView](http://resource.dopus.com/t/command-scrollselectionintoview/18619/1):
>
> ScrollSelectionIntoView - A small script command to automatically scroll the file display to the current selection. I use it when toggling between details and thumbnails mode to keep the selected items visible. It won't do anything if nothing is sel…

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

```auto
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:

```auto
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":

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

Examples - some more:

Select a random item:

```auto
SelectRandom
```

Select 10 random items:

```auto
SelectRandom ITEMCOUNT=10
```

Select 10 random folders:

```auto
SelectRandom FOLDERS ITEMCOUNT=10
```

Select 50% of files:

```auto
SelectRandom FILES ITEMCOUNTPERC=50
```

Select a random number of random items:

```auto
SelectRandom RANDOM
```

Select 1-5 random items:

```auto
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

```auto
SelectRandom FILES SINGLEFOLDER ACTION=dblclk
```

Another Demo:  
Select 50% of items

 ![](https://resource.dopus.com/uploads/default/original/2X/b/b4bcfc4054645a1045f022f06e93fa2d9bc6b9f1.png)  
  
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:

- Latest: v0.3 / 2015.04.19 - new features "NAME, "RECURSE", "JSFILTER", "MIN/MAXSIZE", "MAXSIZETOTAL", "COPYTOCOLL", "ECHO":  
[Command.Generic\_SelectRandom.js.txt](https://resource.dopus.com/uploads/default/original/3X/2/6/26fb0d75b0a8a1d0618c7ec3f0f706b0b68f7a46.txt) (37.1 KB)

- v0.2 / 2015.04.16 - new options "PATH", "SETENVVAR", "SETTABVAR":  
[Command.Generic\_SelectRandom.js.txt](https://resource.dopus.com/uploads/default/original/3X/f/7/f7951d9b36c12b7ee61e084a79d19e5c1082f755.txt) (19.4 KB)

- v0.1.1 - support for updates/scriptwizard:  
[Command.Generic\_SelectRandom.js.txt](https://resource.dopus.com/uploads/default/original/3X/b/b/bbf0aa65f64a575af020d93091fc9edb25a660f9.txt) (6.76 KB)

- v0.1 - initial version:  
[Command.Generic\_SelectRandom.js.txt](https://resource.dopus.com/uploads/default/original/3X/c/b/cbcb6cdb48988601e65604943fe5d401848c36c4.txt) (5.7 KB)

---

<div class="post-metadata">

**Author:** ![Crash](https://resource.dopus.com/user_avatar/resource.dopus.com/crash/32/23921_2.png) [@Crash](https://resource.dopus.com/u/Crash)\
**Post date:** [September 28, 2014, 9:16am UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/2 "2014-09-28T09:16:47Z")

</div>

This is awesome. Thanks for sharing, tbone!  
👍

---

<div class="post-metadata">

**Author:** ![tbone](https://resource.dopus.com/user_avatar/resource.dopus.com/tbone/32/25726_2.png) [@tbone](https://resource.dopus.com/u/tbone)\
**Post date:** [September 28, 2014, 9:37am UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/3 "2014-09-28T09:37:03Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Crash](https://resource.dopus.com/user_avatar/resource.dopus.com/crash/32/23921_2.png) [@Crash](https://resource.dopus.com/u/Crash)\
**Post date:** [September 29, 2014, 12:05am UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/4 "2014-09-29T00:05:56Z")

</div>

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 🙂 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 🙂

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!

---

<div class="post-metadata">

**Author:** ![myarmor](https://resource.dopus.com/user_avatar/resource.dopus.com/myarmor/32/35118_2.png) [@myarmor](https://resource.dopus.com/u/myarmor)\
**Post date:** [January 1, 2015, 8:37am UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/5 "2015-01-01T08:37:53Z")

</div>

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):

```auto
SELECTRANDOM FILES ACTION=dblclk
```

---

<div class="post-metadata">

**Author:** ![tbone](https://resource.dopus.com/user_avatar/resource.dopus.com/tbone/32/25726_2.png) [@tbone](https://resource.dopus.com/u/tbone)\
**Post date:** [January 3, 2015, 1:48pm UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/6 "2015-01-03T13:48:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon94230625](https://resource.dopus.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@anon94230625](https://resource.dopus.com/u/anon94230625)\
**Post date:** [January 13, 2015, 10:52pm UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/7 "2015-01-13T22:52:48Z")

</div>

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. 😢

---

<div class="post-metadata">

**Author:** ![tbone](https://resource.dopus.com/user_avatar/resource.dopus.com/tbone/32/25726_2.png) [@tbone](https://resource.dopus.com/u/tbone)\
**Post date:** [January 15, 2015, 7:07pm UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/8 "2015-01-15T19:07:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anon94230625](https://resource.dopus.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@anon94230625](https://resource.dopus.com/u/anon94230625)\
**Post date:** [January 16, 2015, 12:51am UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/9 "2015-01-16T00:51:34Z")

</div>

Yes, good idea!

---

<div class="post-metadata">

**Author:** ![tbone](https://resource.dopus.com/user_avatar/resource.dopus.com/tbone/32/25726_2.png) [@tbone](https://resource.dopus.com/u/tbone)\
**Post date:** [April 16, 2015, 1:27am UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/10 "2015-04-16T01:27:27Z")

</div>

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.

```javascript
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"));

```

---

<div class="post-metadata">

**Author:** ![tbone](https://resource.dopus.com/user_avatar/resource.dopus.com/tbone/32/25726_2.png) [@tbone](https://resource.dopus.com/u/tbone)\
**Post date:** [April 19, 2015, 12:40pm UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/11 "2015-04-19T12:40:33Z")

</div>

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).

```javascript
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)

---

<div class="post-metadata">

**Author:** ![tbone](https://resource.dopus.com/user_avatar/resource.dopus.com/tbone/32/25726_2.png) [@tbone](https://resource.dopus.com/u/tbone)\
**Post date:** [May 19, 2015, 11:18pm UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/12 "2015-05-19T23:18:49Z")

</div>

"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)](https://resource.dopus.com/t/command-selectex-extended-select-command/20517/1)

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

```nohighlight
SelectRandom <your options here>

```

into

```nohighlight
SelectEx RANDOM <your same options here>

```

Thanks! o)

---

<div class="post-metadata">

**Author:** ![fona](https://resource.dopus.com/letter_avatar_proxy/v4/letter/f/3bc359/32.png) [@fona](https://resource.dopus.com/u/fona)\
**Post date:** [March 20, 2022, 5:18pm UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/13 "2022-03-20T17:18:08Z")

</div>

> [@tbone](#):
>
> COPYTOCOLL - copy results to a file collection

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.

---

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [March 20, 2022, 6:29pm UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/14 "2022-03-20T18:29:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fona](https://resource.dopus.com/letter_avatar_proxy/v4/letter/f/3bc359/32.png) [@fona](https://resource.dopus.com/u/fona)\
**Post date:** [March 20, 2022, 6:48pm UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/15 "2022-03-20T18:48:34Z")

</div>

> [@Leo](#):
>
> 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"),

---

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [March 20, 2022, 7:10pm UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/16 "2022-03-20T19:10:47Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![fona](https://resource.dopus.com/letter_avatar_proxy/v4/letter/f/3bc359/32.png) [@fona](https://resource.dopus.com/u/fona)\
**Post date:** [March 21, 2022, 12:54am UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/17 "2022-03-21T00:54:24Z")

</div>

> [@tbone](#):
>
> COPYTOCOLL

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?

---

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [March 21, 2022, 2:11am UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/18 "2022-03-21T02:11:14Z")

</div>

> [@fona](#):
>
> Simultaneously, I would like the selected 5 images to be moved to the existing folder named "slideshow".

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.

---

<div class="post-metadata">

**Author:** ![fona](https://resource.dopus.com/letter_avatar_proxy/v4/letter/f/3bc359/32.png) [@fona](https://resource.dopus.com/u/fona)\
**Post date:** [March 21, 2022, 2:16pm UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/19 "2022-03-21T14:16:59Z")

</div>

> [@Leo](#):
>
> 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 😀. 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

---

<div class="post-metadata">

**Author:** ![fona](https://resource.dopus.com/letter_avatar_proxy/v4/letter/f/3bc359/32.png) [@fona](https://resource.dopus.com/u/fona)\
**Post date:** [April 29, 2022, 12:56pm UTC](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289/20 "2022-04-29T12:56:01Z")

</div>

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.

[Next page](https://resource.dopus.com/t/command-selectrandom-randomly-select-files-folders/19289.md?page=2)
