Robocopy Drive Mirror

This script creates a lightweight directory mirror of the active drive. The mirror sits in the default location /dropbox\Mirror and consists of the drive label as top folder and all files and folders below it. All files are empty and have a size of zero. The mirror also contains some info files regarding the drive's space.

A mirror like this is most useful as a catalog for external drives, but also works well as a test case for copy and rename buttons (as long as they don't use metadata which is absent).

The script uses the Windows system tool Robocopy.exe. The core functionality is in just one line, the rest is simply putting together the parameters. If you are into scripting, you might find the parts that collect the drive info interesting.

In my experience Dropbox works really well even with big mirrors. Any future run of the script will only add/change/remove the files that are affected, thus keeping the number of files to be synced small.

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

    var mirrorPath = fsu.Resolve('/dropbox\\Mirror');  // Change here as needed; use double backslashes

    tab.path.Root();
    var srcPath = tab.path;

    if (srcPath.drive == 0) {
        dlg.Request('Please select a drive!', 'OK');
        return;
    }

    if (srcPath.drive == mirrorPath.drive) {
        dlg.Request('Please do not select the mirror drive!', 'OK');
        return;
    }

    var srcDrive = fso.GetDrive(srcPath);
    var srcLabel = srcDrive.VolumeName;
    var oneGB = 1024 * 1024 * 1024;
    var srcFree = Math.round(srcDrive.FreeSpace / oneGB);
    var srcTotal = Math.round(srcDrive.TotalSize / oneGB);
    var dstPath = mirrorPath + '\\' + srcLabel;

    cmd.RunCommand('CreateFolder NAME="' + dstPath + '"');
    cmd.RunCommand('Go PATH="' + srcPath + '" DUALPATH="' + dstPath + '"');
    if (tab.right) cmd.RunCommand('Go SWAP');

    cmd.RunCommand('Delete FILE="' + dstPath + '\\*.txt" QUIET');

    wsh.Run('Robocopy.exe ' + srcPath + ' "' + dstPath + '" /S /CREATE /PURGE /NOCOPY', 1, true); // no quotes around srcPath because it ends with backslash
    
    cmd.RunCommand('Print FOLDER="' + srcPath + '" TO="' + dstPath + '\\dirlist.txt" AS=txt FLATVIEW=no CALCSIZES=yes QUIET');
    cmd.RunCommand('FileType NEW=.txt PATH="' + dstPath + '" NEWNAME="norename:' + srcFree + ' GB free.txt"');
    cmd.RunCommand('FileType NEW=.txt PATH="' + dstPath + '" NEWNAME="norename:' + srcTotal + ' GB total.txt"');
    cmd.RunCommand('FileType NEW=.txt PATH="' + dstPath + '" NEWNAME="norename:' + (srcTotal - srcFree) + ' GB used.txt"');
    
    cmd.RunCommand('Delete FILE="' + dstPath + '\\$RECYCLE.BIN" QUIET');
    cmd.RunCommand('Delete FILE="' + dstPath + '\\System Volume Information" QUIET');
}

Robocopy Drive Mirror.dcf (4.3 KB)

1 Like