Move marked files to folders according to their endings

EDIT: Added fix for files matching multiple extensions.

Here's a simple script, where the scripting itself is as simple as possible:

function OnClick(clickData)
{
	var tab = clickData.func.sourcetab;
	var cmd = clickData.func.command;
	cmd.deselect = false; // Prevent automatic deselection

	if (cmd.files.count == 0)
	{
		cmd.AddFiles(tab.files);
	}

	cmd.AddLine('Rename TYPE=files WHENEXISTS=rename REGEXP PATTERN=".+\\.(jpg|png)$" TO="Pictures/\\0"');
	cmd.AddLine('Rename TYPE=files WHENEXISTS=rename REGEXP PATTERN=".+\\.(txt|doc|pdf)$" TO="Documents/\\0"');
	cmd.Run();
}

Here's a slightly more complex script which does the same thing, but gives you a nice, easy-to-edit list of directories and extensions at the top:

var Folders =
{
	"Pictures":  [ "jpg", "png" ],
	"Documents": [ "doc", "txt", "pdf" ],
};

function OnClick(clickData)
{
	var tab = clickData.func.sourcetab;
	var cmd = clickData.func.command;
	cmd.deselect = false; // Prevent automatic deselection

	if (cmd.files.count == 0)
	{
		cmd.AddFiles(tab.files);
	}

	for (var folder in Folders)
	{
		var pattern = ".+\\.(";
		var extensions = Folders[folder];
		for (var i in extensions)
		{
			if (i != 0)
				pattern += "|";
			pattern += extensions[i];
		}
		pattern += ")$";
		var cmdLine = 'Rename TYPE=files WHENEXISTS=rename REGEXP PATTERN="' + pattern + '" TO="' + folder + '/\\0"';
		cmd.AddLine(cmdLine);
	}
	cmd.Run();
}

Same script as a .dcf file that's easier to add to your toolbar:

2 Likes