ConvertArchive (Convert archives to a different format)

ConvertArchive copies the content of selected archives into new archive containers in a different format (7z, RAR, ZIP). The original archives are not deleted. Archives that already have a target archive will be skipped. The add-in uses the internal Copy command for all conversions.

Arguments

  • FORMAT Determines the container format. Uses the same syntax as the ARCHIVE argument. If omitted, .zip is used.

  • TO Determines the target directory. If omitted, the source directory is used.

  • COPYTIMESTAMPS Copies the created and modified timestamps to the new archive container.

  • DRYRUN Creates and logs the internal commands without executing them.

  • NOLOG Disables logging. This is overridden by DRYRUN.

Examples

ConvertArchive FORMAT=.7z                   // create 7z
ConvertArchive FORMAT=.zip,split:100kb      // create zip, split into .z* files
ConvertArchive TO={destpath}
ConvertArchive COPYTIMESTAMPS

How to set up and use

:one: Save CommandConvertArchive.js.txt to   ↓

%appdata%\GPSoftware\Directory Opus\Script AddIns

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

Helpful reading

How to use buttons and scripts from this forum

The script's inner workings

JScript
function OnInit(initData) {
    initData.name = 'ConvertArchive';
    initData.version = '2025-12-20';
    initData.url = 'https://resource.dopus.com/t/convertarchive-convert-archives-to-a-different-format/58113';
    initData.desc = 'Convert archives to a different format (7z, RAR, ZIP)';
    initData.default_enable = true;
    initData.min_version = '12.0';
}

function OnAddCommands(addCmdData) {
    var cmd = addCmdData.AddCommand();
    cmd.name = 'ConvertArchive';
    cmd.method = 'OnConvertArchive';
    cmd.desc = 'Convert archives to a different format (7z, RAR, ZIP)';
    cmd.label = 'ConvertArchive';
    cmd.template = '' +
        'format/o[.7z,.rar,.zip],' +
        'copytimestamps/s,' +
        'to/o,' +
        'nolog/s,' +
        'dryrun/s';
    cmd.hide = false;
    cmd.icon = 'script';
}

function OnConvertArchive(scriptCmdData) {
    var cmd = scriptCmdData.func.command;
    var tab = scriptCmdData.func.sourcetab;
    var args = scriptCmdData.func.args;
    var fsu = DOpus.FSUtil();

    var datetimePrint = 'D#yyyy-MM-dd T#MHH:mm:ss';  // milliseconds

    cmd.deselect = false;

    if (tab.selected_files.count == 0) {
        Log('No files selected.');
        return;
    }

    var archiveFormat = args.format || '.zip';
    var targetExt = archiveFormat.split(',')[0];

    cmd.ClearFiles();

    Log('Enumerating...' + (args.dryrun ? ' (dry run)' : ''));

    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
        Log('');

        var item = e.item();

        if (!item.InGroup('Archives')) {
            Log('Skipped (Not an archive): ' + item);
            continue;
        }

        var newArchive = fsu.Resolve((args.to || item.path) + '\\' + item.name_stem + targetExt);

        if (fsu.Exists(newArchive)) {
            Log('Skipped (Target exists):  ' + item);
            continue;
        }

        cmd.AddFile(item);

        var tmpDirPath = fsu.GetTempDirPath();

        var cmdLine = 'Copy' +
            ' FILE="' + item + '"' +
            ' EXTRACT=sub' +
            ' TO="' + tmpDirPath + '"';

        Run(cmdLine);

        var cmdLine = 'Copy' +
            ' MOVE' +
            ' FILE="' + tmpDirPath + '\\' + item.name_stem + '"' +
            ' ARCHIVE=' + archiveFormat +
            ' TO="' + newArchive.pathpart + '"';

        Run(cmdLine);

        if (args.copytimestamps) {
            var cmdLine = 'SetAttr' +
                ' FILE="' + newArchive + '"' +
                ' CREATED="' + item.create.Format(datetimePrint) + '"' +
                ' MODIFIED="' + item.modify.Format(datetimePrint) + '"';

            Run(cmdLine);
        }
    }

    Log('');
    Log('... done.');

    cmd.RunCommand('Select NONE');
    cmd.RunCommand('Select FROMSCRIPT SETFOCUS');
    cmd.RunCommand('Select SHOWFOCUS');


    // === Helper functions ===

    function Log(str) {
        if (args.nolog && !args.dryrun) return;
        DOpus.Output(str);
    }

    function Run(cmdLine) {
        Log(cmdLine);
        if (args.dryrun) return;
        cmd.RunCommand(cmdLine);
    }
}
8 Likes

Nice!

What if one would like the option to delete the original archive once the conversion was completed/confirmed?

The archives that actually get converted will be selected once the script is finished - just hit delete. I am always a bit nervous when auto-deleting files but I'd wouldn't mind adding a delete option. Are you brave enough?

1 Like

There's a fine line between brave and foolish. I walk it all too often... :wink:

2 Likes

Alright, here's a new version that adds DELETEORIGINAL. You can also pass QUIET to suppress the confirmation dialog and NORECYCLE to skip the recycle bin.

I'll update the main post in a few days if nobody gets hurt :wink:

Enjoy the walk!

CommandConvertArchive.js.txt (2025-12-21)

2 Likes

:joy: agreed. Auto-delete is great but scary. Got to double-check the duplicates! Before it's sent to the bin.

Balancing bravery with common sense, I tied the new version to a button as follows:

@keydown:none
ConvertArchive Format=.zip
@keydown:ctrl
ConvertArchive DELETEORIGINAL Format=.zip
@keydown:shift
ConvertArchive DELETEORIGINAL NORECYCLE Format=.zip

Works well!

Thanks, as always, @lxp !

2 Likes