//v0.1 - o9/2o14 //- initial //v0.1.1 //- support for automatic updates/scriptwizard //v0.2 //- new option "PATH" to choose items from any folder //- new option "SETENVVAR" the given env-var will contain item-paths //- new option "SETTABVAR" the given tab-var will contain item-paths //v0.3 //- new option "NAME" to filter items by name/extension (regular expression) //- new option "RECURSE" recursive mode, meant to be used with PATH //- new option "JSFILTER" for custom scripting filter logic //- new option "MINSIZE" to filter files by minimum file size //- new option "MAXSIZE" to filter files by maximum file size //- new option "MAXSIZETOTAL" to limit total size //- new option "COPYTOCOLL" to copy results to a file collection //- new switch "ECHO" to output results to the console for easier testing //todo: //- put result into txtfile? /////////////////////////////////////////////////////////////////////////////// function OnInit(data){ //uid added via script wizard (do not change after publishing this script) var uid = "18C9CF48-05DB-4B9F-B293-9F5D692C266A"; //resource center url added via script wizard (required for updating) var url = "http://resource.dopus.com/viewtopic.php?f=35&t=23142"; data.name = "Command.Generic: SelectRandom"; data.desc = "Randomly retrieve/select a (random) number of items."; data.copyright = "tbone"; data.version = "0.3"; data.default_enable = true; data.min_version = "11"; var cmd = data.AddCommand(); cmd.name = "SelectRandom" cmd.method = "Command_SelectRandom"; cmd.desc = data.desc; cmd.template = "PATH/K,RECURSE/S,SETTABVAR/K,SETENVVAR/K,FOLDERS/S,FILES/S,NODESELECT/S,RANDOM/S,SINGLEFOLDER/S,SINGLEFOLDERMINCOUNT/K,ITEMCOUNT/K,ITEMCOUNTPERC/K,ACTION/K,NAME/K,MINSIZE/K,MAXSIZE/K,MAXSIZETOTAL/K,JSFILTER/K,ECHO/S,COPYTOCOLL/K,COMMANDS/R"; /////////////////////////////////////////////////////////////////////////// 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=[this.data.config[name],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[1])=description; return this;} this.val = function(val){ if (typeof this.last[0]=="object") this.last[0].push_back(val); else this.last[0]=val; return this;} } /////////////////////////////////////////////////////////////////////////// var cfg = new ConfigHelper(data); cfg.add("DebugOutput", false). des("Enable debug output in the script console."); } /////////////////////////////////////////////////////////////////////////////// var COMMAND_ABORTED = true; var COMMAND_SUCCEEDED = false; /////////////////////////////////////////////////////////////////////////////// function Command_SelectRandom(data){ Debug("SelectRandom():"); var fso = new ActiveXObject("Scripting.FilesystemObject"); var params = { path : GetParameter(data, "PATH", null, false), recurse : GetParameter(data, "RECURSE", null, false), setTabVar : GetParameter(data, "SETTABVAR", null, ""), setEnvVar : GetParameter(data, "SETENVVAR", null, ""), folders : GetParameter(data, "FOLDERS", null, null), files : GetParameter(data, "FILES", null, null), noDeselect : GetParameter(data, "NODESELECT", null, false), random : GetParameter(data, "RANDOM", null, false), singleFol : GetParameter(data, "SINGLEFOLDER", null, false), singleFolMin : GetParameter(data, "SINGLEFOLDERMINCOUNT", null, 1), action : GetParameter(data, "ACTION", null, ""), itemCount : GetParameter(data, "ITEMCOUNT", null, 1), itemCountPerc : GetParameter(data, "ITEMCOUNTPERC", null, null), commands : GetParameter(data, "COMMANDS", null, ""), name : GetParameter(data, "NAME", null, null), minSize : GetParameter(data, "MINSIZE", null, null), maxSize : GetParameter(data, "MAXSIZE", null, null), maxSizeTotal : GetParameter(data, "MAXSIZETOTAL", null, null), jsFilter : GetParameter(data, "JSFILTER", null, null), copyToColl : GetParameter(data, "COPYTOCOLL", null, null), echo : GetParameter(data, "ECHO", null, false) } /////////////////////////////////////////////////////////////////////////// function FriendlySizeToBytes(friendlySize){ var size = parseFloat(friendlySize=friendlySize.toLowerCase()); if (isNaN(size)) return null; if (friendlySize.match(/(\d|\s)(b|bytes?)$/)) ; else if (friendlySize.match(/(kb|kilobytes?)$/)) size*=1024; else if (friendlySize.match(/(mb|megabytes?)$/)) size*=1024*1024; else if (friendlySize.match(/(gb|gigabytes?)$/)) size*=1024*1024*1024; else if (friendlySize.match(/(tb|terabytes?)$/)) size*=1024*1024*1024*1024; else if (friendlySize.match(/(pb|petabytes?)$/)) size*=1024*1024*1024*1024*1024; else if (friendlySize.match(/(eb|exabytes?)$/)) size*=1024*1024*1024*1024*1024*1024; else size = null; return size; } /////////////////////////////////////////////////////////////////////////// function Filter( item, itemSize, nameWild, minSize, maxSize, file, folder, jsFilter){ if (nameWild && !nameWild.Match(item.name)) return "name"; if (minSize!==null && minSize>itemSize) return "minsize"; //(!item.is_dir || (item.is_dir && item.got_size)) if (maxSize!=null && maxSizemaxSizeTotal)) { i--; continue; } } sizeTotal+=itemSize; cmd.AddFile(item); } return cmd; } /////////////////////////////////////////////////////////////////////////// function GetNumberOfItemsToSelect(params, items){ if (params.itemCountPerc && !isNaN(parseInt(params.itemCountPerc,10))) { params.itemCountPerc = parseInt(params.itemCountPerc); if (params.itemCountPerc>0) params.itemCount = items.count*(params.itemCountPerc/100); } if (params.itemCount > items.count) params.itemCount = items.count; if (params.random){ if (params.itemCount>1) params.itemCount = Math.floor((Math.random()*params.itemCount)+1); else params.itemCount = Math.floor((Math.random()*items.count)+1); } return params.itemCount; } /////////////////////////////////////////////////////////////////////////// function ItemCollectionToCSVLine(collection, separator){ separator = separator || ";" var itemsSeparated = ""; for (var iEnum = new Enumerator(collection); !iEnum.atEnd();){ var item = iEnum.item(); iEnum.moveNext(); itemsSeparated += item.realpath + (!iEnum.atEnd()?separator:""); } return itemsSeparated; } /////////////////////////////////////////////////////////////////////////// function GetCollectionToPickFrom(filteredItemCollections, params){ if (params.singleFol && (params.singleFolMin <= filteredItemCollections.dirs.count)){ params.random = false; params.itemCount = 1; params.itemCountPerc = null; return filteredItemCollections.dirs; } if (params.folders && !params.files) return filteredItemCollections.dirs; if (params.files && !params.folders) return filteredItemCollections.files; return filteredItemCollections.all; } /////////////////////////////////////////////////////////////////////////// function SelectItemsInFiledisplay(data, params, cmd){ if (!params.path){ DOpus.Output(" Selecting items.."); cmd.RunCommand("Select FROMSCRIPT EXACT"+(!params.noDeselect?" DESELECTNOMATCH":"")); return true; } return false; } /////////////////////////////////////////////////////////////////////////// function SetReturnStringOfItems(data, params, cmd){ if (params.setTabVar || params.setEnvVar){ SetResultVariables(data, params, ItemCollectionToCSVLine(cmd.files, ";")); return true; } return false; } /////////////////////////////////////////////////////////////////////////// function TriggerFileTypeActions(params, cmd){ if (params.action){ Debug(" Triggering action ["+params.action+"].."); cmd.RunCommand("FileType ACTION="+params.action); return true; } return false; } /////////////////////////////////////////////////////////////////////////// function ExecuteCommands(params, cmd){ if (params.commands){ Debug(" Running commands.."); var commands = params.commands.split("\\n"); for(var c=0;c maxLen) maxLen = param.length; for(param in params){ var pad = ""; while (pad.length+param.length