Merge Folders and Collections

MergeFolders combines the contents of selected folders or file collections into a chosen destination, which can be selected from a drop-down menu.

The Switch button alternates the drop-down menu display between the folders' names and their paths.

How to set up and use

:one: Save CommandMergeFolders.js.txt to   

%appdata%\GPSoftware\Directory Opus\Script AddIns

:two: Add the new command to a button, hotkey, context menu, etc. like any built-in command, or run it from the FAYT Command field.

Things you might enjoy reading

How to use buttons and scripts from this forum

The script's inner workings

JScript
function OnInit(initData) {
    initData.name = 'MergeFolders';
    initData.version = '2024-02-23';
    initData.url = 'https://resource.dopus.com/t/merge-folders-and-collections/41476';
    initData.desc = 'MergeFolders';
    initData.default_enable = true;
    initData.min_version = '12.0';
}

function OnAddCommands(addCmdData) {
    var cmd = addCmdData.AddCommand();
    cmd.name = 'MergeFolders';
    cmd.method = 'OnMergeFolders';
    cmd.desc = 'Merge Folders and Collections';
    cmd.label = 'MergeFolders';
    cmd.hide = false;
    cmd.icon = 'script';
}

function OnMergeFolders(scriptCmdData) {
    var cmd = scriptCmdData.func.command;
    var tab = scriptCmdData.func.sourcetab;
    var dlg = scriptCmdData.func.Dlg();
    var fsu = DOpus.FSUtil();

    cmd.deselect = false;

    if (tab.selected_dirs.count < 2) {
        dlg.Request('Please select at least two folders!', 'OK');
        return;
    }

    var dirItems = DOpus.Create().Vector();
    var dirNames = DOpus.Create().Vector();
    for (var e = new Enumerator(tab.selected_dirs); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        dirItems.push_back(item);
        dirNames.push_back(item.name);
    }

    dlg.title = 'Merge Folders';
    dlg.message = 'Please select the destination folder:';
    dlg.buttons = 'OK|&Switch|Cancel';
    dlg.selection = 0;
    var showPath = false;
    do {
        dlg.choices = showPath ? dirItems : dirNames;
        var userChoice = dlg.Show();
        if (userChoice == 2) showPath = !showPath;
    } while (userChoice == 2);

    if (userChoice != 1) return;

    var s = dlg.selection;
    var dstPath = dirItems(s).path;
    var dstFolder = dirItems(s).name;
    var dstColl = dirItems(s);
    dirItems.erase(s);

    if (String(dstPath).substring(0, 5) == 'coll:') {
        for (var e = new Enumerator(dirItems); !e.atEnd(); e.moveNext()) {
            var coll = e.item();
            cmd.SetFiles(fsu.ReadDir(coll).Next(-1));
            cmd.RunCommand('Copy' +
                ' TO="' + dstColl + '"' +
                ' COPYTOCOLL=member');
        }
        cmd.SetFiles(dirItems);
        cmd.RunCommand('Delete QUIET');
    } else {
        cmd.SetFiles(dirItems);
        cmd.RunCommand('Copy' +
            ' MOVE' +
            ' AS="' + dstFolder + '"' +
            ' TO="' + dstPath + '"' +
            ' WHENEXISTS=rename,merge');
        cmd.RunCommand('Go' +
            ' PATH="' + dstPath + '\\' + dstFolder + '"' +
            ' OPENCONTAINER');
    }
}
12 Likes

Thanks for the script, bookmarked and added as a new button which may come in handy anytime soon!

Thankou very much for this script.
It works very well as far as I have been brave enough to test it.

I'm not certain how it will eventually end up on a toolbar.
One Directory Opus Command that may be useful with this is
'Close Alllisters=collapse' .

The command Collapses the tabs of all listers to the active lister and closes all but the active lister.
Left Tabs are rendered left and Right Tabs are rendered right, but the listers merge to one Lister.

I think it can be merged with New Folder to upgrade the function of this button:

  1. When no file or folder is selected, click this button to create a new folder, similar to Ctrl+Shift+N
  2. When a file or folder is selected, click this button to bring up a dialog box to add the option of whether or not the text of the new folder is added, check the box to create a new file, the title of the dialog box is New Folder, and the text box You can edit the name of the new folder, or drop down to select the name of the selected file or folder.

Update 2024-02-23

  • Converted button to script command for easier handling and integration into other buttons
  • No change in functionality
3 Likes

I've created multi-functional buttons before, but ended up not using them because I couldn't remember the exact function of my ingenious inventions :blush:

There are other buttons like this available that offer to create or rename folders but I purposefully excluded this. The script selects the destination folder after completion - hit F2 to rename it.

However, the script is open-source, so feel free to modify it and expand the options!