Folder packer+

Folder Packer+


In short this script takes selected files and folders and packs them into one or more folders depending on the mode being used. Please read below for details on the operation modes.

This script seeks to improve the functionality over existing folder packing functions. It can be used as a right click context menu item or as a button.


Operation Modes


This script has 3 different modes, default, manual, and split pack

Default

- This mode is enabled by default. Creates a folder in the current directory named after the first selected item and moves all selected items into that folder. If attached to a context menu item, the first selected is always the item you right clicked. For buttons the first selected is usually the top left most selected item.
Visual demonstration

default

Manual

- Enabled by holding Shift while activating. The user will be presented with a folder dialog to select where they would like to pack selected folders / files to.
Visual demonstration

manual

Split-pack

- Enabled by holding Ctrl when activating. Instead of packing selected items into one folder in the current directory, it instead packs each selected item into it's own folder. Each folder will bear the same name as the item it contains.
Visual demonstration

split-pack


Both manual and split pack mode can be combined although do note this will result in one selected item being packed to the selected destination and the rest in the current folder.

Additional Features


This script also supports the following:

Folder Merging

- If the user attempts to pack files to an already existing directory under the current folder, instead of creating a new folder those files will instead be packed into that existing folder. For example, if you select 5 folders and 3 files and your first selected item is folder 1, all files will be packed into folder one. If the first selected item is a file named "dddd" and there is already a folder named "dddd", all selected items will be packed into that existing directory.
Visual demonstration

folder-merge


There have also been improvements to folder packing over a regular function implementation (example below) in regards to preventing errors. For example, if you attempt to pack a file containing any trailing periods via the typical folder packing function you receive an error. A regular implementation also receives an error whenever attempting to pack files / folders into the first selected item as it attempts to move the folder into itself instead of merging like this script.
Example regular pack to folder function
@set dir={file|noext}
Copy MOVE HERE CREATEFOLDER="{$dir}"

Script

Two versions of the script offered. One that doesn't output detailed info to the script log in DOPUS and one that does. For most the one without extra info should be fine.

Without detailed output
function OnClick(clickData)
{
    var cmd = clickData.func.command;
    var oDir = ""; // Dir to pack files to
    var cDir = ""; // Dir to pack files to for split pack mode
    var hKeys = clickData.func.qualifiers;

    var i = 0;
    cmd.clear();
    cmd.AddLine("@Set dir=\{file|noext\}");
    for (var e = new Enumerator(cmd.files); !e.atEnd(); e.moveNext()) {   
        if (i == 0){ // First iteration setup
            if (hKeys.match(".*shift.*")){ // User names the dir if shift held

                var dlg = clickData.func.dlg;
                var DF = DOpus.create();
                oDir = dlg.folder("Please select a folder to pack files to: ", e.item().path, true);
             } else { // otherwise use filename as dir name
                oDir = e.item().path + "\\" + e.item().name_stem.replace(/[\.\s]*\.+[\.\s]*$/g, "");
            }
            if (DOpus.fsUtil.Exists(oDir) == false){ // Dir doesn't exist to pack files to so create it
                cmd.AddLine("CreateFolder \"" + oDir + "\" NOSEL");
                cmd.AddLine("Copy \"" + e.item() + "\" TO \"" + oDir + "\" MOVE WHENEXISTS=ask");
            } else { // Folder already exists
                if (e.item().is_dir){ // Current item is indeed the folder we are packing files to
 
                } else {
                    cmd.AddLine("Copy \"" + e.item() + "\" TO \"" + oDir + "\" MOVE WHENEXISTS=ask");
                }
            }
        } else { // 2nd and later iterations
            if (hKeys.match(".*ctrl.*")) { // Ctrl key held for split pack mode (each item to separate folder)
                cDir = e.item().path + "\\" + e.item().name_stem.replace(/[\.\s]*\.+[\.\s]*$/g, "");
                if (DOpus.fsUtil.Exists(cDir) == false){ // Dir doesn't exist to pack files to so create it
                    cmd.AddLine("CreateFolder \"" + cDir + "\" NOSEL");
                    cmd.AddLine("Copy \"" + e.item() + "\" TO \"" + cDir + "\" MOVE WHENEXISTS=ask");
                } else { // Folder already exists
                    if (e.item().is_dir){ // Current item is indeed the folder we are packing files to

                    } else {
                        cmd.AddLine("Copy \"" + e.item() + "\" TO \"" + cDir + "\" MOVE WHENEXISTS=ask");
                    }
                }
            } else { // Pack remaining files to oDir
                cmd.AddLine("Copy \"" + e.item() + "\" TO \"" + oDir + "\" MOVE WHENEXISTS=ask");
            }
        }
        i++;
    }
    cmd.Run();
    cmd.clear();
}
with detailed output
function OnClick(clickData)
{
    var cmd = clickData.func.command;
    var oDir = ""; // Dir to pack files to
    var cDir = ""; // Dir to pack files to for split pack mode
    var hKeys = clickData.func.qualifiers;

    var i = 0;
    cmd.clear();
    cmd.AddLine("@Set dir=\{file|noext\}");
    for (var e = new Enumerator(cmd.files); !e.atEnd(); e.moveNext()) {   
        if (i == 0){ // First iteration setup
            if (hKeys.match(".*shift.*")){ // User names the dir if shift held
                DOpus.output("The shift key was being held down at launch");
                var dlg = clickData.func.dlg;
                var DF = DOpus.create();
                oDir = dlg.folder("Please select a folder to pack files to: ", e.item().path, 200, "Confirm", "Name Folder");
                //DOpus.output(dlg.input);
                //var uInput = DF.stringTools.MakeLegal(dlg.input, 's');
               // DOpus.output(uInput);
             } else { // otherwise use filename as dir name
                oDir = e.item().path + "\\" + e.item().name_stem;
            }
            DOpus.output("Directory to pack folders to: " + oDir);
            if (DOpus.fsUtil.Exists(oDir) == false){ // Dir doesn't exist to pack files to so create it
                cmd.AddLine("CreateFolder \"" + oDir + "\" NOSEL");
                cmd.AddLine("Copy \"" + e.item() + "\" TO \"" + oDir + "\" MOVE WHENEXISTS=ask");
            } else { // Folder already exists
                DOpus.output("Folder already exists");
                if (e.item().is_dir){ // Current item is indeed the folder we are packing files to
                    DOpus.output("Folder of the same name already exists");
                } else {
                    cmd.AddLine("Copy \"" + e.item() + "\" TO \"" + oDir + "\" MOVE WHENEXISTS=ask");
                }
            }
        } else { // 2nd and later iterations
            if (hKeys.match(".*ctrl.*")) { // Ctrl key held for split pack mode (each item to separate folder)
                cDir = e.item().path + "\\" + e.item().name_stem;
                if (DOpus.fsUtil.Exists(cDir) == false){ // Dir doesn't exist to pack files to so create it
                    cmd.AddLine("CreateFolder \"" + cDir + "\" NOSEL");
                    cmd.AddLine("Copy \"" + e.item() + "\" TO \"" + cDir + "\" MOVE WHENEXISTS=ask");
                } else { // Folder already exists
                    DOpus.output("Folder already exists");
                    if (e.item().is_dir){ // Current item is indeed the folder we are packing files to
                        DOpus.output("Folder of the same name already exists");
                    } else {
                        cmd.AddLine("Copy \"" + e.item() + "\" TO \"" + cDir + "\" MOVE WHENEXISTS=ask");
                    }
                }
            } else { // Pack remaining files to oDir
                DOpus.output("File / Folder name: " + e.item());
                cmd.AddLine("Copy \"" + e.item() + "\" TO \"" + oDir + "\" MOVE WHENEXISTS=ask");
            }
        }
        i++;
    }
    cmd.Run();
    cmd.clear();
}

Button

Same as above, two versions of the button are offered. One that doesn't output info to the script log in DOPUS and one that does. For most the one without detailed output should be fine.

Pack.dcf (4.7 KB)
Pack (detailed output).dcf (5.3 KB)


All comments and suggestions for improvement welcome!

6 Likes

Very thorough documentation. Might I suggest adding to the Description field of the .dcf the following text so the user is reminded of the keystroke options:

None: Move All Items to First of Selected Items, Shift: Ask for Folder, Ctrl: Each Selected Item in its Own Folder

1 Like

Good suggestion, I'm updating them right now.