Script to update labels for a mirrored backup copy

Below is a script to automatically and recursively update all file and folder labels for a mirrored copy.

Why this is useful: if you keep duped backup copies of your files and they have DOpus labels assigned you cannot easily update them to your mirror copy with usual file compare utilities (I use BeyondCompare myself) as the labels are not stored within the file proper and so changes are not detected by the compare programs. This script allows you to keep your labels synched manually. The script will open the other log window while running and show a "Finished" message when it's done.

To run this script you need to have

  1. the original and the mirror open in the two tabs (sourcetab and desttab)
  2. exactly mirrored sub-file path structures and filenames
  3. MAKE ABSOLUTELY SURE THE ACTIVE TAB IS THE ONE WITH THE ORIGINAL/MASTER COPY OF THE FILES. If you reverse this you would irreversibly mirror the labels of the destination to the source and screw up your original copy's labels. Experiment with the script on a test folder first to figure out how it works.
function OnClick(clickData) { 
	DOpus.ClearOutput() // clear output log

	cmd = clickData.func.command; 
	cmd.RunCommand("Set UTILITY=OtherLog,Toggle"); 	

	ProcessFolder (clickData.func.sourcetab.path,clickData.func.desttab.path, 0);
	DOpus.Output("Finished");
}


function ProcessFolder (sourcePath, destPath, totalCount) {
	var cnt = 0;
	var tot = totalCount;

	var enumFiles = DOpus.FSUtil.ReadDir(sourcePath, false);
    while (!enumFiles.complete && (fItem = enumFiles.Next())) {
        var itemLabels = fItem.Labels();
        if (itemLabels.count > 0) {
			var oppItemLabels = DOpus.FSUtil.GetItem(destPath+'\\'+fItem.name).Labels();
			var update = false;
			if (oppItemLabels.count != itemLabels.count){
				update = true;
			}	
			else{
				for (var i = 0; i<itemLabels.count ;i++) {
					if (itemLabels(i) != oppItemLabels(i)){
						update = true;
						break;
					}
				}
			}
			
			if (update == true) {
				var doCmd = DOpus.NewCommand;
				doCmd.AddLine("Properties SETLABEL !reset");
				for (var e = new Enumerator(itemLabels); !e.atEnd(); e.moveNext()) {
					doCmd.AddLine("Properties ADDLABEL SETLABEL=\""+e.item()+"\"");
				}	
							
				doCmd.AddFile(destPath+'\\'+fItem.name);
				doCmd.Run();
				cnt++;
			}
		}
		if (fItem.is_dir) {
			tot += ProcessFolder (sourcePath+'\\'+fItem.name, destPath+'\\'+fItem.name, tot); // recurse
		}
    }

	tot += cnt;
	if (cnt > 0) DOpus.Output("Processed "+sourcePath+" updated: "+cnt+" total labels updated: "+ tot);

	return tot - totalCount;
}