Button to rename files, sorting components of their names

Here's a Rename Preset which does that:

For reference:

function OnGetNewName(getNewNameData)
{
	// Separate name and extension.
	var stem = getNewNameData.item.name_stem;
	var ext = getNewNameData.item.ext;

	// Separate anything after the last " - ".
	var suffix = "";
	var suffixPos = stem.lastIndexOf(" - ");
	if (suffixPos != -1)
	{
		suffix = stem.substr(suffixPos);
		stem = stem.substr(0, suffixPos);
	}

	// Sort components between ", "
	stem = stem.split(", ").sort().join(", ");

	// Put everything back together
	return stem + suffix + ext;
}
2 Likes