// 202000611 v1.0 Purpose: To prefix image/movie files with a date and time stamp // 202000611 v1.0 whilst retaining the original file name stem in (brackets) for easy restoration // 202000611 v1.0 Uses date taken metadata if available, otherwise uses date modified // 202000611 v1.0 e.g. myfile.jpg is renamed to 20200609_123456_tkn_(myfile).jpg or 20200609_123456_mod_(myfile).jpg // 202000611 v1.0 where the _tkn_ or _mod_ timestamp suffix indicates which one was used // 202000611 v1.0 Use the REVERT option to restore original names from within (parentheses) // 202000611 v1.0 Use with ctrl key qualifier to open a configuration dialog // 202000611 v1.0 Use with shift key qualifier to move files with yyyymmdd_ prefix to YYYY/MM subfolders //-- Global Variables var me = { name:"RenameToTaken", version:"1.0 (June 11th, 2020)", copyright:"(c) 2020 Aussie", minver:"12" } var d, dbg, dlg, trc; var sfx_taken, sfx_modified; //-- Initialise configuration variables, etc. function OnInit(initData) { initData.name = me.name; initData.version = me.version; initData.copyright = me.copyright; initData.default_enable = true; var cmd = initData.AddCommand(); cmd.name = me.name; cmd.method = "do_run"; cmd.label = me.name; cmd.template = "revert/s,move/s,cfg/s"; initData.config_desc = DOpus.create.map(); initData.config.dbg = true; initData.config_desc("dbg") = 'Write trace information to "Other" log?'; initData.config.trc = 1; initData.config_desc("trc") = 'How much to write to "Other" log. Bigger number = more.'; initData.config.sfx_taken = "tkn"; initData.config_desc("sfx_taken") = "Date taken suffix. Can be left blank if not required."; initData.config.sfx_modified = "mod"; initData.config_desc("sfx_modified") = "Date modified suffix. Can be left blank if not required."; initData.config.shiftmove = true; initData.config_desc("shiftmove") = "Move to YY/MM structure when shift qualifier key is pressed."; initData.config.ctrlcfg = true; initData.config_desc("ctrlcfg") = "Open configuration when ctrl qualifier key is pressed."; } String.prototype.trim = function(s){s=s||"\\s";return this.replace(new RegExp("^("+s+"){1,}\|("+s+"){1,}$","g"),"");} //-- Main script code function do_run(ScriptCommandData){ // Define shortcuts to a few objects d = ScriptCommandData; dlg = d.func.dlg; var cmd = DOpus.Create.Command; // Process configuration variables and initialise key variables dbg = Script.config.dbg; trc = Script.config.trc; sfx_taken = Script.config.sfx_taken; sfx_modified = Script.config.sfx_modified; // Who am I? log("version "+me.version+ " starting.."); // Check minimum version requirements if (!isVersionOK(me.minver)) return; // Might be a request to configure if (isConfigRequest()) return; // Might be a request to revert to original names if (isRevertRequest()) return; // Process selected files; only interested in images and movies var items = d.func.sourcetab.selected_files; log("No of candidate files selected = "+items.count,1); var cmdstring, n = 0; for (var it = 0; it < items.count; it++){ var fileItem = items(it); // Retrieve the next selected file if (!isEligible(fileItem)) continue; // Ignore if it is ineligible n++; // Increment eligible file counter var dt = fileItem.metadata.image.datetaken; // Retrieve date taken information, if present // Use modified date if date taken is N/A var str = (typeof dt=="object") ? dt.format("D#yyyyMMdd_T#HHmmss_")+sfx_taken : fileItem.modify.format("D#yyyyMMdd_T#HHmmss_")+sfx_modified; str = 'Rename "'+fileItem+'" TO "'+str+"_("+fileItem.name_stem+")"+fileItem.ext.toLowerCase(); cmdstring = str.replace("__(","_("); // Handle blank sfx_taken or sfx_modified log(cmdstring,2); cmd.runcommand(cmdstring); } log("No of candidate files renamed = "+n,1); // Check to see if a move to YY/MM has been requested if ((d.func.args.got_arg.move) || ((d.func.qualifiers=="shift") && (Script.config.shiftmove))){ d.func.sourcetab.Update(); // Some file names may have been changed var yy,mm,strPath,strPathSfx; items = d.func.sourcetab.selected_files; n = 0; for (var it = 0; it < items.count; it++){ var fileItem = items(it); // Retrieve the next selected file if (0!=fileItem.name.search(/\d{8}_\d{6}_/)) continue; // Ignore if it is ineligible yy = fileItem.name.slice(0,4); mm = fileItem.name.slice(4,6); strPath = String(fileItem.path); strPathSfx = "\\"+yy+"\\"+mm; // Only move to YY\MM if not already there if (strPath.lastIndexOf(strPathSfx)+strPathSfx.length!=strPath.length){ n++; // Increment eligible file counter cmdstring = 'Rename "'+fileItem.name+'" TO '+yy+'/'+mm+'/* AUTORENAME'; log(cmdstring,2); cmd.runcommand(cmdstring); // Use Rename to move to YY/MM structure } } log("No of eligible files moved to YY/MM folders = "+n,1); } return; } function log(str,i){ if (!dbg) return; if (String(str).trim()=="") return false; var lvl = (i==undefined) ? 0 : i; if (trc0)) return false; if ((sfx_modified!="") && (str.indexOf("_"+sfx_modified+"_(")>0)) return false; return true; } function isRevertRequest(){ if (!d.func.args.got_arg.revert) return false; var cmd = DOpus.Create.Command; // See https://resource.dopus.com/t/rename-with-regex-stop-at-first-occurrence/35790/3 var cmdstring = 'Rename PATTERN ".+\\((.+?)[\\.\\)](.*)\\.(.+)" TO "\\1.\\3" REGEXP AUTORENAME'; log(cmdstring,1); cmd.runcommand(cmdstring); return true; } function isVersionOK(candidate){ var isOK = DOpus.version.AtLeast(candidate); // Check minimum version requirements if (!isOK){ var str = "DOpus version "+candidate+" is required."; dlg.request(str,"OK","Version Check Failure"); log(str); } return isOK; }