# Rename directory after largest child file

**URL:** https://resource.dopus.com/t/rename-directory-after-largest-child-file/33244
**Category:** Rename Presets
**Created:** [August 9, 2019, 2:28pm UTC](https://resource.dopus.com/t/rename-directory-after-largest-child-file/33244 "2019-08-09T14:28:35Z")
**Posts on this page:** 1
**Page:** 1

<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: [August 9, 2019, 2:28pm UTC](https://resource.dopus.com/t/rename-directory-after-largest-child-file/33244/1 "2019-08-09T14:28:36Z")

</div>

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](https://resource.dopus.com/t/renamining-directories-after-file-names/33230), which has more background on what it's for.

Here's the rename preset:

- [Dir to Largest Child.orp](https://resource.dopus.com/uploads/short-url/7q8ch1aP27mbEu0vqMhKMUmOnS5.orp) (887 Bytes)
- [How to use Rename Presets from this forum](https://resource.dopus.com/t/how-to-use-rename-presets-from-this-forum/26726)

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

```javascript
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;
}

```
