Robocopy Folder Mirror Archive

This script creates in the source a zip-archive that contains a lightweight mirror of the selected files and folders. All files in the archive are empty and have a size of zero. It is helpful if you quickly want to share test cases or want to get support for e.g. file renaming. Note that the files contain no metadata other than what is stored in the file system.

Sound familiar? Yep, it's this script's little brother.

// https://resource.dopus.com/t/robocopy-folder-mirror-archive/39032

// Version 2021-08-06
// - abort, if nothing is selected

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    var fsu = DOpus.FSUtil();
    var wsh = new ActiveXObject('WScript.Shell');
    cmd.deselect = false;

    if (tab.selected.count == 0) return;

    var srcPath = String(tab.path).replace(/\\$/, ''); // to make Robocopy work with files in root
    var tmpPath = fsu.GetTempDirPath();
    var zipStem = 'RFM-' + DOpus.Create().Date().Format('D#yyyyMMdd-T#HHmmss');

    DOpus.ClearOutput();

    for (var e = new Enumerator(tab.selected_dirs); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        var cmdLine = 'Robocopy.exe "' + item + '" "' + tmpPath + '\\' + item.name + '" /E /CREATE';
        DOpus.Output(cmdLine);
        wsh.Run(cmdLine, 0, true);
    }

    if (tab.selected_files.count > 0) {
        var cmdLine = 'Robocopy.exe "' + srcPath + '" "' + tmpPath + '"';
        for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
            var item = e.item();
            cmdLine += ' "' + item.name + '"';
        }
        cmdLine += ' /CREATE';
        DOpus.Output(cmdLine);
        wsh.Run(cmdLine, 0, true);
    }

    cmd.RunCommand('Copy ARCHIVE=keepfolder FILE="' + tmpPath + '\\*" TO="' + srcPath + '" CREATEFOLDER="' + zipStem + '"');

    cmd.RunCommand('GO PATH="' + srcPath + '\\' + zipStem + '.zip" OPENCONTAINER NEWTAB OPENINDUAL');
    cmd.RunCommand('GO PATH="' + tmpPath + '" NEWTAB=nofocus OPENINDUAL');
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>Robocopy Folder Mirror Archive</label>
	<icon1>#copy</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/robocopy-folder-mirror-archive/39032</instruction>
		<instruction />
		<instruction>// Version 2021-08-06</instruction>
		<instruction>// - abort, if nothing is selected</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    var fsu = DOpus.FSUtil();</instruction>
		<instruction>    var wsh = new ActiveXObject(&apos;WScript.Shell&apos;);</instruction>
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    if (tab.selected.count == 0) return;</instruction>
		<instruction />
		<instruction>    var srcPath = String(tab.path).replace(/\\$/, &apos;&apos;); // to make Robocopy work with files in root</instruction>
		<instruction>    var tmpPath = fsu.GetTempDirPath();</instruction>
		<instruction>    var zipStem = &apos;RFM-&apos; + DOpus.Create().Date().Format(&apos;D#yyyyMMdd-T#HHmmss&apos;);</instruction>
		<instruction />
		<instruction>    DOpus.ClearOutput();</instruction>
		<instruction />
		<instruction>    for (var e = new Enumerator(tab.selected_dirs); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>        var item = e.item();</instruction>
		<instruction>        var cmdLine = &apos;Robocopy.exe &quot;&apos; + item + &apos;&quot; &quot;&apos; + tmpPath + &apos;\\&apos; + item.name + &apos;&quot; /E /CREATE&apos;;</instruction>
		<instruction>        DOpus.Output(cmdLine);</instruction>
		<instruction>        wsh.Run(cmdLine, 0, true);</instruction>
		<instruction>    }</instruction>
		<instruction />
		<instruction>    if (tab.selected_files.count &gt; 0) {</instruction>
		<instruction>        var cmdLine = &apos;Robocopy.exe &quot;&apos; + srcPath + &apos;&quot; &quot;&apos; + tmpPath + &apos;&quot;&apos;;</instruction>
		<instruction>        for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>            var item = e.item();</instruction>
		<instruction>            cmdLine += &apos; &quot;&apos; + item.name + &apos;&quot;&apos;;</instruction>
		<instruction>        }</instruction>
		<instruction>        cmdLine += &apos; /CREATE&apos;;</instruction>
		<instruction>        DOpus.Output(cmdLine);</instruction>
		<instruction>        wsh.Run(cmdLine, 0, true);</instruction>
		<instruction>    }</instruction>
		<instruction />
		<instruction>    cmd.RunCommand(&apos;Copy ARCHIVE=keepfolder FILE=&quot;&apos; + tmpPath + &apos;\\*&quot; TO=&quot;&apos; + srcPath + &apos;&quot; CREATEFOLDER=&quot;&apos; + zipStem + &apos;&quot;&apos;);</instruction>
		<instruction />
		<instruction>    cmd.RunCommand(&apos;GO PATH=&quot;&apos; + srcPath + &apos;\\&apos; + zipStem + &apos;.zip&quot; OPENCONTAINER NEWTAB OPENINDUAL&apos;);</instruction>
		<instruction>    cmd.RunCommand(&apos;GO PATH=&quot;&apos; + tmpPath + &apos;&quot; NEWTAB=nofocus OPENINDUAL&apos;);</instruction>
		<instruction>}</instruction>
	</function>
</button>

https://resource.dopus.com/t/how-to-use-buttons-and-scripts-from-this-forum/3546

4 Likes

Very nice @lxp. I would add a test, as follows, to gracefully handle the situation when the user has omitted to select anything.

    var tab = clickData.func.sourcetab;
	if (tab.selected.count==0) return;
1 Like

Good point! Done :slight_smile: