//'Move to Server' in JavaScript function OnInit(data) { //uid added via script wizard (do not change after publishing this script) var uid = "EEA95F2A-8BA6-4FD1-AEA8-0824B65A3321"; //resource center url added via script wizard (required for updating) var url = "http://resource.dopus.com/viewtopic.php?f=35&t=23585"; // Set the script details etc data.name = "Move to Server"; data.desc = "A command to move selected files to predefined locations"; data.copyright = "© 2014 Rebel154"; data.version = "1.0"; data.default_enable = true; data.min_version = "11.7.1"; data.default_enable = true; var cmd = data.AddCommand(); cmd.name = "MoveToServer"; cmd.desc = data.desc; cmd.method = "OnMoveToServer"; cmd.label = "Move To Server"; /////////////////////////////////////////////////////////////////////////////// // Many thanks to 'tbone' for this function which eases the creation of config items. function ConfigHelper(data) { this.data=data; this.descriptions=null; this.last=null; this.add = function(name, val, description) { this.data.config[name]=val; this.last=name; if (description!=undefined) this.des(description); return this; } this.des = function(description) { if (!(description && DOpus.version.AtLeast("11.6.1"))) return this; if (!this.descriptions) { this.descriptions=DOpus.NewMap(); data.config._descriptions=this.descriptions; } this.descriptions(this.last)=description; return this; } this.val = function(val) { if (typeof this.data.config[this.last]=="object") this.data.config[this.last].push_back(val); else this.data.config[this.last]=val; return this; } } /////////////////////////////////////////////////////////////////////////////////// var cfg = new ConfigHelper(data); /////////////////////////////////////////////////////////////////////////////////// cfg.add("Testing", true, "Change to 'False' to actually Copy/Delete real files!!"); cfg.add("DeleteFiles", false, "Change this to 'True' to delete the source files after they have been copied. A confirmation dialog will pop up if 'ConfimDelete' is 'true'"); cfg.add("ConfirmDelete", true, "Change to 'False' to bypass 'Confirm Delete' prompt. Files will be deleted without warning if 'DeleteFiles' is 'true' and 'ConfirmDelete' is 'false'."); cfg.add("Root", "D:\\", "The base location where files will be copied to. The location must already exist."); cfg.add("keyLength", 3, "Set this to the number of characters required to find a unique start of each selected file name"); cfg.add("DebugOutput", false).des("Enable debug output in the script console."); cfg.add("FindVersion",true,"Should the script attempt to find the selected file(s) version number?"); cfg.add("FileStem", DOpus.NewVector()).des("Define the 1st 3 (or keyLength value) unique chars of filename and matching desination folder. Folders must already exist."); } ////////////////////////////////////////////////////////////////////////////////// function OnMoveToServer(data) { var dlg = data.func.dlg; var cmd = data.func.command; var deleteCon = 0; var selFiles = []; // Selected filenames var desFile; // final filename var desFolder; //final destination folder var desFilePath; //complete final filename path var userVer; //user entered version number var userDest; // user entered destination folder var selStem; //1st 3 (or keyLength) chars of selected filename(s) //Check that only files are selected. if (data.func.sourcetab.selected_dirs.count > 0) { dlg.request("Only files should be selected", "OK"); return; } else if (data.func.sourcetab.selected_files.count == 0) { dlg.request("Please select some files.", "OK"); return; } cmd.ClearFiles; // Are we just testing the script (results sent to DOpus Script Output log) if (Script.config.Testing == false) { //Not testing if (Script.config.ConfirmDelete == true && Script.config.DeleteFiles == true) { // Confirm deleteion of files deleteCon = dlg.request("Source Files will be deleted - Confirm", "OK|NO!"); } else if (Script.config.DeleteFiles == false) { //No files will be deleted dlg.request("Source Files will not be deleted", "OK"); } else { deleteCon = 1; //Files will be deleted without warning } } // Enumerate the selected files so we can access their properties (name etc) var objEnum = new Enumerator(data.func.sourcetab.selected_files); if (!objEnum.atEnd()); { var x = 0; while (!objEnum.atEnd()) { selFiles[x] = objEnum.item(); ++x; objEnum.moveNext(); } } DOpus.ClearOutput // clear the log screen for testing /* Look for the version number in the filename using a Regular Expression to determine: a. Are there any numbers followed by a dot in the filename - if not bypass file. b. Trim the trailing dot from found version string and if 3 or less chars, ignore. c. Put the version number into a new string */ for (var i in selFiles) { // check each file in turn if (Script.config.FindVersion == true) { var nameVers = ""; var test = /[0-9]+\./.test(selFiles[i].name); // check for number(s) followed by dot in filename if (test == true) { var vers = /([0-9]+\.)+/g.exec(selFiles[i].name); //returns array of matches nameVers = vers[0].substr(0,vers[0].length - 1); //get the first match and remove trailing 'dot' if (nameVers.length <= 3) { // avoids '64.' as in DOpusInstall64.exe nameVers = ""; } } // Get the version string from file properties if it exists var prodVers = selFiles[i].metadata.exe.prodversion; var startPos = selFiles[i].name.indexOf(prodVers); // Is prodversion already in filename, if so get it's start position if (nameVers != "" && startPos == -1) { desFile = selFiles[i].name; } else { if (prodVers != null && prodVers != "0.0.0.0") { // some files (MediaMonkey for one) have "0.0.0.0" in the version property of the file desFile = selFiles[i].name_stem + "_" + prodVers + selFiles[i].ext; } else if (prodVers == undefined || prodVers == "0.0.0.0") { userVer = dlg.GetString("Enter Version # for " + selFiles[i].name,"",10,"OK|Cancel","Version Number Required"); if (userVer == null) { desFile = selFiles[i].name; } else { desFile = selFiles[i].name_stem + "_" + userVer + selFiles[i].ext; } } } } else { desFile = selFiles[i].name; } selStem = desFile.slice(0,Script.config.keyLength); // get 1st 3 (or whatever keyLength is set to) chars of selected filename // Thanks to 'tbone' for suggesting this bit of code to split the stem and folder parts var fileStems = Script.config["FileStem"]; for (var c = 0 ; c < fileStems.length ; c++) { if (fileStems(c).substring(0,2) == "//") continue; var parts = fileStems(c).split(":"); if (selStem == parts[0]) { desFolder = Script.config.Root + parts[1] + "\\"; break; // if a match is found, drop out of the 'for' loop } } if (selStem != parts[0]) { // if no match, ask user for destination folder userDest = dlg.folder("Select folder for " + desFile,Script.config.Root,1); if (userDest.result == false) { desFolder = Script.config.Root; } else { desFolder = userDest + "\\" } } desFilePath = "\"" + desFolder + desFile + "\""; // only needed for testing purposes to show complete path in Output window if (Script.config.Testing == false) { cmd.AddFile(selFiles[i]); cmd.RunCommand("Copy WHENEXISTS=SKIP AS " + "\"" + desFile + "\"" + " To=" + "\"" + desFolder + "\""); if (deleteCon == 1) { cmd.RunCommand("Delete QUIET"); } cmd.ClearFiles; } else { DOpus.Output(desFilePath); } } } /////////////////////////////////////////////////////////////////////////////// function OnAboutScript(data){ //v0.1 var cmd = DOpus.Create.Command(); if (!cmd.Commandlist('s').exists("ScriptWizard")){ if (DOpus.Dlg.Request("The 'ScriptWizard' add-in has not been found.\n\n"+ "Install 'ScriptWizard' from [resource.dopus.com].\nThe add-in enables this dialog and also offers "+ "easy updating of scripts and many more.","Yes, take me there!|Cancel", "No About.. ", data.window)) cmd.RunCommand('http://resource.dopus.com/viewtopic.php?f=35&t=23179');} else cmd.RunCommand('ScriptWizard ABOUT WIN='+data.window+' FILE="'+Script.File+'"'); } //MD5 = "913d1e4a0009388eb5b61b10d3da5c71"; DATE = "2014.12.13 - 19:27:14"