Move all subfolder files to the current / selected folder (Flatten folders) (Unpack folders)

/*
===============================================================================
Function : Move files in all subfolders to the current folder, auto rename, delete empty folders, two-step undo
Created  : 2022-11-08
Modified : 2023-09-04
Version  : v0.5
===============================================================================
*/


    var tab = DOpus.listers(0).activetab;
    var cmd = DOpus.Create().Command;
	var fsu = DOpus.FSUtil;
	var soucepath = tab.path;
    var dirs = tab.selected_dirs;

if (dirs.count != 0) {
// Enumerate and add all files in each selected folder
    cmd.ClearFiles();
    for (var eSel = new Enumerator(dirs); !eSel.atEnd(); eSel.moveNext() )
    {
		var eFolder = eSel.item();
		var folderEnum = fsu.ReadDir(eFolder, "r");
	    while (!folderEnum.complete) {
		    var item = folderEnum.Next;
		    if (!item.is_dir)
			    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 folders size
	cmd.ClearFiles();
    for (var eSel = new Enumerator(dirs); !eSel.atEnd(); eSel.moveNext() )
    {
		 var eItem = eSel.item();
		 if (!FileCountRecursive(eItem))
		     cmd.AddFile(eItem);
	}

// 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;
}

Toolbar button:
Unpack folders v0.5.dcf (3.5 KB)
Unpack folders .dcf (3.0 KB)

==============================================================================
Move all subfolder files to the selected folders

/*
===============================================================================
Function : Move all subfolder files to the selected folders, auto rename, delete empty folders, two-step undo
Created  : 2023-08-21
Modified : 2023-08-21
Version  : v0.1
=============================================================================
*/


    var tab = DOpus.listers(0).activetab;
    var cmd = DOpus.Create().Command;
    var dirs = tab.selected_dirs;

// @dirsonly
if (dirs.count != 0) {
    cmd.ClearFiles();
    for (var eSel = new Enumerator(dirs); !eSel.atEnd(); eSel.moveNext() )
    {
		 var eFolder = eSel.item();
		 cmd.AddFile(eFolder);
	}

// Move the subfolder's items to the selected directory via Rename operation
    cmd.RunCommand('Rename PATTERN="*" TO="{filepath$}*" AUTORENAME RECURSE=HERE');

// Calculate the subfolder size
	cmd.ClearFiles();
    for (var eSel = new Enumerator(dirs); !eSel.atEnd(); eSel.moveNext() )
    {
	     var eFolder = eSel.item();
	     var folderEnum = DOpus.FSUtil.ReadDir(eFolder);
		 while (!folderEnum.complete) {
		     var folderItem = folderEnum.Next;
			 if (folderItem.is_dir)
		         if (!FileCountRecursive(folderItem))
		             cmd.AddFile(folderItem);
		 }
	}

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

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

Toolbar button: Move all subfolder files to the selected folders
Unpack subfolders.dcf (3.4 KB)

5 Likes

Is this different to the more simple Move everything up command from the pinned Quick buttons for directory organisation post?

The Delete command has a SKIPNOTEMPTY argument which should make that a one-liner.

I modified the topic, similar to this script:
Command:Unpack folders with one step undoable - Buttons/Scripts - Directory Opus Resource Centre (dopus.com)
But there seems to be something wrong with this script.

Yes, I tried and tried some combinations, but it says that an error occurred.

It helps if you tell us what the error was, and also what the command was. :slight_smile:

Ok I have time to report it now, start a new topic.
Edit:
This is not an issue. . .
I want to delete the folders to the recycle.
image

Hi @WKen, your code is very useful, congratulations!!!

I replaced in the line: cmd.RunCommand('Rename PATTERN="*" TO="{filepath|..}*" AUTORENAME RECURSE HERE');

"{filepath|..}*" with "{filepath$}*"

So that the files are moved to the root of the selected folder, and I managed to move the files, but empty subfolders are not removed.

Before

Folder ABC (selected folder)
    Sudfolder 1
        File 1
        File 2
    Sudfolder 2
        File 3
        File 4

After

Folder ABC  (selected folder)
    Sudfolder 1 (empty)
    Sudfolder 2 (empty)
         File 1
         File 2
         File 3
         File 4

Could you help me to remove these empty subfolders? Thank you so much!

Perfect @WKen , thank you very much, it has left me very happy! :grinning: