Extract archives with condition?

Hello all,

I usually have one rule when I extract an archived folder. When I open the zipped folder, if there is ONLY ONE folder in there, then I always "Extract here". If there's a more than one, or anything else, then I extract to "sub".

Is it possible to create a script, command or button that will do that for me? In other words, it will look in the archive, and extract one way if true, and extract the other way if false? That way, I don't have to open and look first, but instead just click it and it will determine that for me.

If it's possible and simple, is there anyone that can write it? I don't know how to write scripts, I just always use what I find here :sweat_smile:

Thanks!

Try this. It'll loop through all selected files.

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    var fsu = DOpus.FSUtil();
    cmd.deselect = false;

    cmd.RunCommand('Set UTILITY=otherlog');
    DOpus.ClearOutput();
    DOpus.Output('Enumerating...\n');

    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        if (!item.InGroup('Archives')) continue;

        var folderEnum = fsu.ReadDir(item);
        var folderItem = folderEnum.Next();
        if (folderItem.is_dir && folderEnum.complete) {
            var cmdLine = 'Copy FILE="' + item + '" HERE WHENEXISTS=rename EXTRACT';
        } else {
            var cmdLine = 'Copy FILE="' + item + '" HERE WHENEXISTS=rename EXTRACT=sub';
        }
        DOpus.Output(cmdLine);
        cmd.RunCommand(cmdLine);
    }
    DOpus.Output('\n... done.');
}

36313.dcf (1.9 KB)


4 Likes

This worked perfectly, thank you so much!!

this is truly the best community out there...

1 Like

Hi!

Sorry to bother again but, is there something I can add to this so that, at the end, it deletes the original archive after it's done archiving?

Insert

cmd.RunCommand('Delete FILE="' + item + '" QUIET');

after

cmd.RunCommand(cmdLine);

Keep in mind that there is no error checking. The archives get deleted whether the extraction was successful or not.

And while you are at it:

Insert

cmd.SetDestTab(tab);

after

cmd.deselect = false;

so it works properly in single lister display.

I haven't tried this with these particular commands, but you might be able to get simple error checking by adding the copy and delete via cmd.AddLine, then running them as a single command via cmd.Run. If the copy fails, the delete shouldn't happen.

But I'd still be careful using a button like that, as "fail" means the command had to be aborted entirely, which will exclude cases where, say, you accidentally click Skip All when you meant to click Replace or Abort, and then nothing is extracted and the archive is deleted... Or antivirus might see a new file and tell you it deleted it, and then the archive also gets deleted...

I much prefer to do the two steps separately, making the extraction leave the archives selected (which lxp's script already does) so I can then click Delete afterwards when/if that's what I want, while staying in full control of what happens, for just one extra click.

Both of you guys's responses have made me think. I guess I hadn't thought about the different scenarios since there is no error checking. I will heed your advice and not add the commands. I can definitely see any of those scenarios occur.

Thank you guys, this is truly the best community out there. I've been using this button since it was posted nad it has saved me TONS of time. Thanks again both of you for your input.

1 Like

This is AMAZING thank you so much for this <3

1 Like

I created an advanced button with script and dialogs. Take a look here.

2 Likes