GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Rename directory after largest child file

This preset renames each selected folder to the same name as the largest file directly below it, with the file extension removed.

Only files directly below each selected folder are considered. If you want the search to be recursive -- i.e. look for the largest file anywhere below each selected folder -- then change ReadDir(item) to ReadDir(item,"r").

Originally posted in Renamining Directories after File Names, which has more background on what it's for.

Here's the rename preset:

Here's the script code inside the preset, for reference:

function OnGetNewName(getNewNameData)
{
	var item = getNewNameData.item;
	if (!item.is_dir)
	{
		return; // Leave files as-is.
	}
	var maxSize = DOpus.FSUtil.NewFileSize(0);
	var maxName = "";
	var folderEnum = DOpus.FSUtil.ReadDir(item);
	while(!folderEnum.complete)
	{
		var childItem = folderEnum.Next;
		if (!childItem.is_dir)
		{
			if (maxName == "" || childItem.size.Compare(maxSize) > 0)
			{
				maxSize.Set(childItem.size);
				maxName = childItem.name_stem_m;
			}
		}
	}
	folderEnum.Close();

	if (maxName == "")
	{
		return; // No child files.
	}

	return maxName;
}