Button: Random Shuffle

Requires Directory Opus 12 or above.

Overview:

This script-button shuffles the files and folders in the tab into a random order each time you click it.

Files and folders are mixed together.

Right-clicking the button resets things back to normal.

You might use this for generating music playlists, or similar.

Installation:

Download Shuffle.dcf, enter Settings > Customize mode, and drag it to a toolbar. Then click OK in the Customize window.

(For more detailed instructions: How to use buttons and scripts from this forum)

History:

  • 1.0 (04/Sep/2016): Initial version.

Script code:

The script code from the download above is reproduced below. This is for people browsing the forum for scripting techniques. You do not need to care about this code if you just want to use the script.

function OnClick(clickData)
{
	var vecNewOrder = DOpus.Create.Vector();
	vecNewOrder.reserve(clickData.func.sourcetab.all.count);
	for (var eItems = new Enumerator(clickData.func.sourcetab.all); !eItems.atEnd(); eItems.moveNext())
	{
		vecNewOrder.push_back(eItems.item().name);
	}

	var currentIndex = vecNewOrder.size;
	var temporaryValue;
	var randomIndex;

	// While there remain elements to shuffle...
	while (0 !== currentIndex)
	{
	    // Pick a remaining element...
	    randomIndex = Math.floor(Math.random() * currentIndex);
	    currentIndex -= 1;

	    // And swap it with the current element.
		vecNewOrder.exchange(currentIndex, randomIndex);
	}

	clickData.func.Command.RunCommand("Set MANUALSORT=On");
	var sortOrder = clickData.func.sourcetab.format.manual_sort_order;
	sortOrder.SetOrder(vecNewOrder);
	clickData.func.Command.RunCommand("Select FIRST SETFOCUS");
	clickData.func.Command.RunCommand("Select SHOWFOCUS");
	clickData.func.Command.RunCommand("Select NONE");
}

The right-click action runs a simple non-script command:

Set MANUALSORT=off 
Select FIRST SETFOCUS
Select SHOWFOCUS
Select NONE

Nice!

Could you add an option for shuffle either files or folders only?

+1 to Sasa's request

I would also appreciate an option to shuffle only files (or only folders) depending on the situation

@Leo: Just to let you know, it works great on Local drives but on network shares it fails to Reset - the folder contents remain shuffled.

It seems fine on network drives here. (I also tried a non-NTFS drive and that was OK as well.)

I was not able to shuffle files when I'm in Flat View (Mixed - No Folders). Is there a way to do it?