A script request

Does somebody have a ready made script that walks through a directory tree, counts files in each directory and if it has less than X items, moves them up one level and deletes the now empty directories? Quick search in the forum did not find anything.
Thanks, Boris

I believe there is a script in this forum which can count the content of the folder and add the content number at the end of the folder name. I Think That Script done half of your want. And only needs add a if argument if content number is lees than X then Moves them One level UP and delete the empty folder, else do nothing.

?

/*
===============================================================================
AUTHOR   : https://resource.dopus.com/t/a-script-request/53754/3
Function : Move up subfolder contents with less than X files: two-step undo
Created  : 2022-11-08
Version  : v0.1 (2024-12-10)
===============================================================================
*/


    var tab = DOpus.listers(0).activetab;
    var cmd = DOpus.Create().Command;
	var fsu = DOpus.FSUtil;
	var soucepath = tab.path;
    var dirs = tab.dirs;   // All folders
	var numb = DOpus.Dlg.GetString("Move up subfolder contents with less than X files", "10")

if (dirs.count != 0 && numb >= 0) {
// Enumerate the contents of each subfolder
    cmd.ClearFiles();
    for (var eSel = new Enumerator(dirs); !eSel.atEnd(); eSel.moveNext() )
    {
		var eFolder = eSel.item();
		if (FileCountRecursive(eFolder) > numb) continue;
		var folderEnum = fsu.ReadDir(eFolder);
	    while (!folderEnum.complete) {
		    var item = folderEnum.Next;
			cmd.AddFile(item)
	    }
	}

// Move the subfolder's items to the current directory via Rename operation
	if (cmd.filecount)
	    cmd.RunCommand('Rename PATTERN="*" TO="' + soucepath + '\\' + '*"' + ' AUTORENAME');

// Calculate subfolder size
	cmd.ClearFiles();
    for (var eSel = new Enumerator(dirs); !eSel.atEnd(); eSel.moveNext() )
    {
		 var eFolder = eSel.item();
		 if (!FileCountRecursive(eFolder))
		     cmd.AddFile(eFolder);
	}

// Delete empty folders
	cmd.RunCommand('Delete FILE RECYCLE QUIET');
}

function FileCountRecursive(filePath) {
	var fileCount = 0;
	var folderEnum = fsu.ReadDir(filePath, "r");
	while (!folderEnum.complete) {
		var folderItem = folderEnum.Next;
		if (!folderItem.is_dir) {
			++fileCount;
		}
	}
	return fileCount;
}
2 Likes

Works like a charm! Thanks!