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.

Update 2022-07-05: Made it easier to pick a target folder other than the source for the zip file.

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

// 2022-07-05

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    var dtab = clickData.func.desttab;
    var dlg = clickData.func.Dlg();
    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');

    // zip goes to (pick one)...

    // ... source
    var zipPath = srcPath;

    // ... destination 
    // var zipPath = dtab.path;

    // ... Desktop 
    // var zipPath = fsu.Resolve('/desktop');

    // ...  user's choice
    // var zipPath = dlg.Folder('Pick target folder for zip file', '/profile', true); 
    // if (!zipPath.result) return; // user has cancelled

    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="' + zipPath + '" CREATEFOLDER="' + zipStem + '"');

    cmd.RunCommand('GO PATH="' + zipPath + '\\' + 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>// 2022-07-05</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    var dtab = clickData.func.desttab;</instruction>
		<instruction>    var dlg = clickData.func.Dlg();</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>    // zip goes to (pick one)...</instruction>
		<instruction />
		<instruction>    // ... source</instruction>
		<instruction>    var zipPath = srcPath;</instruction>
		<instruction />
		<instruction>    // ... destination </instruction>
		<instruction>    // var zipPath = dtab.path;</instruction>
		<instruction />
		<instruction>    // ... Desktop </instruction>
		<instruction>    // var zipPath = fsu.Resolve(&apos;/desktop&apos;);</instruction>
		<instruction />
		<instruction>    // ...  user&apos;s choice</instruction>
		<instruction>    // var zipPath = dlg.Folder(&apos;Pick target folder for zip file&apos;, &apos;/profile&apos;, true); </instruction>
		<instruction>    // if (!zipPath.result) return; // user has cancelled</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; + zipPath + &apos;&quot; CREATEFOLDER=&quot;&apos; + zipStem + &apos;&quot;&apos;);</instruction>
		<instruction />
		<instruction>    cmd.RunCommand(&apos;GO PATH=&quot;&apos; + zipPath + &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

6 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:

excellent

Hi @lxp

This is great! Been trying to figure out how to end up with this output!

One small thing, is there (how?) a way to change the script to ask for the destination for the Zip file rather than the source (this may be mounted read only in my scenario!) or alternatively send it to the destination lister?

Cheers

Valid point! I've added options to easily change the target folder for the zip file.

1 Like

Awesome. Thank you!