CopyFlat (Flat Copy selection without Flat View)

CopyFlat copies all selected files and folders flat to the destination.

The script analyses the selection and internally flattens all folders (moves all files to the top level).

It then runs the Copy command with all arguments from the original command line.

This means:

  1. All arguments available to Copy can be used for CopyFlat.
  2. The selection will be ignored if the FROM argument is used.

CopyFlat is similar to copying from a tab in Flat View, but doesn't require Flat View to be on. This is helpful if you only want to pick a few folders and Flat View is impractical because of the number of folders involved or the type of drive.

Examples:

// analyse selection and copy to destination
CopyFlat

// analyse selection and move to D:\heyho
CopyFlat MOVE TO=D:\heyho 
function OnInit(initData) {
    initData.name = 'CopyFlat';
    initData.version = '2023-03-21';
    initData.copyright = 'CopyFlat';
    initData.url = 'https://resource.dopus.com/t/copyflat-flat-copy-selection-without-flat-view/44001';
    initData.desc = 'CopyFlat';
    initData.default_enable = true;
    initData.min_version = '12.0';
}

function OnAddCommands(addCmdData) {
    var cmd = addCmdData.AddCommand();
    cmd.name = 'CopyFlat';
    cmd.method = 'OnCopyFlat';
    cmd.desc = 'CopyFlat';
    cmd.label = 'CopyFlat';
    cmd.template = '';
    cmd.hide = false;
    cmd.icon = 'script';
}

function OnCopyFlat(scriptCmdData) {
    var cmd = scriptCmdData.func.command;
    var fsu = DOpus.FSUtil();
    var log = true; // set to false to avoid logging

    cmd.deselect = false;

    if (log) {
        cmd.RunCommand('Set UTILITY=otherlog');
        DOpus.ClearOutput();
        DOpus.Output('Enumerating...');
        DOpus.Output('');
    }

    var flatFiles = DOpus.Create().Vector();

    for (var e = new Enumerator(cmd.files); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        if (item.is_dir) {
            var folderEnum = fsu.ReadDir(item, 'r');
            while (!folderEnum.complete) {
                var folderItem = folderEnum.Next();
                if (folderItem.is_dir) continue;
                flatFiles.push_back(folderItem);
                if (log) DOpus.Output(flatFiles.count + '  ' + folderItem);
            }
            folderEnum.Close();
        } else {
            flatFiles.push_back(item);
            if (log) DOpus.Output(flatFiles.count + '  ' + item);
        }
    }

    cmd.SetFiles(flatFiles);

    if (log) DOpus.Output('');
    if (log) DOpus.Output('Processing ' + cmd.filecount + ' files...');
    var cmdLine = scriptCmdData.cmdline.replace(/copyflat/i, 'Copy');
    if (log) DOpus.Output(cmdLine);
    cmd.RunCommand(cmdLine);
    if (log) DOpus.Output('');
    if (log) DOpus.Output('... done.');
}

Save CommandCopyFlat.js.txt to

%appdata%\GPSoftware\Directory Opus\Script AddIns

and add the new command to a button, hotkey, context menu, etc. like any built-in command.


How to use buttons and scripts from this forum

7 Likes

Although I really like this script, I do not really get the meaning behind it.

I've read the posting that lead me to this script here, but even the initial request does not enlighten my mind :grin:

Take a deep breath and a few test folders and give it try :wink:

2023-03-22 - 13.09.24

sounds like very useful to me, thanks for the script