// RenameMatchingExtended // (c) 2019 wowbagger // This is a script for Directory Opus. // See https://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information. // Called by Directory Opus to initialize the script function OnInit(initData) { initData.name = "RenameMatchingExtended"; initData.version = "1.0"; initData.copyright = "(c) 2019 wowbagger"; // initData.url = "https://resource.dopus.com/viewforum.php?f=35"; initData.desc = "OnRename rename similar items"; initData.default_enable = true; initData.min_version = "12.0"; initData.config_desc = DOpus.NewMap(); // Set DEBUG flag below to true in order to enable logging messages to the Opus Output Window initData.config.DEBUG = false; initData.config_desc("DEBUG") = "Set DEBUG flag to true in order to enable logging messages to the Opus Output Window."; initData.config.enabled_var_key = "enablerenamematching"; initData.config_desc("enabled_var_key") = "Variable key ($glob:enablerenamematching) that can be used to enable the script"; } // Called to provide notification that a file operation is complete function OnFileOperationComplete(fileOperationCompleteData) { var objCmd = DOpus.CreateCommand; if (IsVariableSet("$lst:" + Script.config.enabled_var_key) || IsVariableSet("$src:" + Script.config.enabled_var_key) || IsVariableSet("$glob:" + Script.config.enabled_var_key)) { return RenameMatchingFiles(fileOperationCompleteData); } } function RenameMatchingFiles(fileOperationCompleteData) { if (fileOperationCompleteData.action == "rename") { if(fileOperationCompleteData.query) { logMsg("query for action " + fileOperationCompleteData.action); logMsg("command: " + fileOperationCompleteData.cmdline); // Only run if not called from this scrupt return fileOperationCompleteData.cmdline.indexOf("SCRIPTARG RenameMatchingExtended:True") == -1; } else { logMsg("rename complete!"); logMsg("command: " + fileOperationCompleteData.cmdline); for (var e = new Enumerator(fileOperationCompleteData.data); !e.atEnd(); e.moveNext()) { var oldPath = e.item(); var newName = fileOperationCompleteData.data(oldPath); logMsg(oldPath + " -> " + newName); var renamedItem = DOpus.FSUtil.GetItem(oldPath); var parentItem = DOpus.FSUtil.GetItem(renamedItem.path); var newNameStem = GetNewNameStem(renamedItem, newName); var oldNameStem = renamedItem.name_stem; //Rename sibling and niblings var folderEnum = DOpus.FSUtil.ReadDir(parentItem, true); while (!folderEnum.complete) { var item = folderEnum.next; CheckAndRename(item, oldNameStem, newNameStem); } //Rename parent CheckAndRename(parentItem, oldNameStem, newNameStem); } } } } function CheckAndRename(itemToRename, oldNamePart, newNamePart) { if(itemToRename.name.indexOf(oldNamePart) != -1) { //using regexp might case issues for special chars var udpatedName = itemToRename.name.replace(oldNamePart, newNamePart); var cmd = "Rename FROM \"" + itemToRename + "\" TO \"" + udpatedName + "\" SCRIPTARG RenameMatchingExtended:True"; logMsg(cmd); DOpus.NewCommand.RunCommand(cmd); } } function GetNewNameStem(renamedItem, newName) { if(renamedItem.is_dir) { return newName; } else { var re = new RegExp("^(.*?)(?:\.[^\.]*)?$"); return newName.replace(re, "$1"); } } function IsVariableSet(variableName) { var objCmd = DOpus.CreateCommand; if (objCmd.IsSet(variableName)) { logMsg("The '" + variableName + "' variable is set!"); return true; } return false; } function IsDebugEnabled() { var objCmd = DOpus.CreateCommand; return (Script && Script.config.DEBUG) || objCmd.IsSet("$glob:" + Script.config.debug_var_key); } function logMsg(message) { if (IsDebugEnabled()) DOpus.Output(message); return; }