// History // See https://resource.dopus.com/t/how-to-navigate-from-a-sub-folder-to-next-or-previous-sub-folder/33930/17 // 20191130 v1.0 // 20191202 v1.1 Fix bug when only 1 item at lowest level in depth > 3 structure //-- Add some useful string methods String.prototype.left = function(n){return this.substring(0,n);} String.prototype.right = function(n){return this.slice(-n);} String.prototype.before = function(s){return this.replace(new RegExp("(.*)("+s+")(.*)"),"$1");} String.prototype.after = function(s){return this.replace(new RegExp("(.*)("+s+")(.*)"),"$3");} String.prototype.from = function(s){return this.replace(new RegExp("(.*)("+s+")(.*)"),"$2$3");} String.prototype.lTrim = function(s){s=s||"\\s";return this.replace(new RegExp("^("+s+"){1,}"),'');} String.prototype.rTrim = function(s){s=s||"\\s";return this.replace(new RegExp("("+s+"){1,}$"),'');} String.prototype.trim = function(s){return this.lTrim(s).rTrim(s);} //-- Global Variables var me = { name:"KHGo", version:"1.1 (December 2nd, 2019)", desc:"Custom folder structure navigator for Khalid Hosain", copyright:"(c) 2019 Aussie", template:"", minver:"12", }; var d, cmd, dbg, dlg, fsu; function OnInit(initData) { initData.name = me.name; initData.version = me.version; initData.desc = me.desc; initData.copyright = me.copyright; initData.default_enable = true; var cmd = initData.AddCommand(); cmd.name = me.name; cmd.method = "do_main"; cmd.desc = me.desc; cmd.label = me.name; cmd.template = me.template; initData.config_desc = DOpus.create.map(); initData.config.dbg = false; initData.config_desc("dbg") = 'Write trace information to "Other" log.'; } //-- Main script code function do_main(ScriptCommandData){ d = ScriptCommandData; dlg = d.func.dlg; fsu = DOpus.FSUtil; cmd = DOpus.Create.Command; // Debug ? dbg = Script.config.dbg; // Who am I? if (dbg) DOpus.output(me.name+" version " + me.version + " starting.."); // Check minimum version requirements if (!DOpus.version.AtLeast(me.minver)){ dlg.request(me.name+" requires DOpus version " + me.minver + " or later","OK","["+me.name+"]"); return; } // Process args me.argstring = d.cmdline.slice(me.name.length).trim(); if (dbg) DOpus.output("argstring = "+me.argstring); // Any supplied argument should be a positive or negative integer e.g. 3 or -2 // if the argument is missing or zero or invalid it defaults to 1. Non integer numbers are rounded down. n = parseInt(me.argstring); if ((isNaN(n)) || (n==0)) n = parseInt(1); if (dbg) DOpus.output("n = " + n); // Process the current folder object var src = d.func.sourcetab; var curitem, curitems = DOpus.create.vector(), curdirs = fsu.readdir(src.path.def_value); while (!curdirs.complete){ curitem = curdirs.next; if ((curitem.is_dir) && (!curitem.fileattr.hidden)) curitems.push_back(curitem); } // Handle current depth = 0 (e.g. starting point is c:\) if (src.path.components==1){ if (curitems.count>1){ var ix = (n<0) ? newix(0,n,curitems.count) : newix(0,n-1,curitems.count); if (dbg) DOpus.output("ix = " + ix); cmd.runcommand("Go "+maybe_quote(curitems(ix))); } return; } var parentitem, parentix, parentitems = DOpus.create.vector(); var parentdirs = fsu.readdir(src.path.pathpart); while (!parentdirs.complete){ parentitem = parentdirs.next; if ((parentitem.is_dir) && (!parentitem.fileattr.hidden)){ if (parentitem==src.path.def_value) parentix = parentitems.count; parentitems.push_back(parentitem); } } if (dbg) DOpus.output("parentitems.count = " + parentitems.count); if (dbg) DOpus.output("parentix = " + parentix); var ix = newix(parentix,n,parentitems.count); // Handle current depth = 1 (e.g. c:\animals) or 2 (e.g. c:\animals\cats) if (src.path.components<4){ if (dbg) DOpus.output("ix = " + ix); cmd.runcommand("Go "+maybe_quote(parentitems(ix))); return; } // Handle current depth > 3 (e.g. c:\animals\cats\burmese) var grandparentitem, grandparentix, grandparentitems = DOpus.create.vector(); var str = src.path.pathpart; if (dbg) DOpus.output("parent = " + str); str = str.slice(0,str.lastIndexOf("\\")); if (dbg) DOpus.output("grandparent = " + str); var grandparentdirs = fsu.readdir(str); while (!grandparentdirs.complete){ grandparentitem = grandparentdirs.next; if ((grandparentitem.is_dir) && (!grandparentitem.fileattr.hidden)){ if (dbg) DOpus.output(grandparentitem); if (grandparentitem==src.path.pathpart) grandparentix = grandparentitems.count; grandparentitems.push_back(grandparentitem); } } if (grandparentitems.count==1) return; if (dbg) DOpus.output("grandparentix = " + grandparentix); // Establish new parent index within grandparent structure var ix = (n>0) ? newix(grandparentix,1,grandparentitems.count) : newix(grandparentix,-1,grandparentitems.count); if (dbg) DOpus.output("ix = " + ix); // Process new parent var newparentitem, newparentix, newparentitems = DOpus.create.vector(); var newparentdirs = fsu.readdir(grandparentitems(ix)); while (!newparentdirs.complete){ newparentitem = newparentdirs.next; if ((newparentitem.is_dir) && (!newparentitem.fileattr.hidden)) newparentitems.push_back(newparentitem); } if (dbg) DOpus.output("newparentitems.count = " + newparentitems.count); newparentix = Math.abs(n) - 1; if (dbg) DOpus.output("newparentix = " + newparentix); if (newparentitems.count>newparentix) cmd.runcommand("Go "+maybe_quote(newparentitems(newparentix))); return; } //-- Functions called from main script code function maybe_quote(str){ // Avoid unnecessary use of quotes and always force to string type return (String(str).indexOf(" ")<0) ? String(str) : '"'+String(str)+'"'; } function newix(cur,inc,n){ // Establish the next index based on a starting point in a 0-base vector // and an integer increment that can be +ve or -ve. n = no of elements in the vector. var next = (cur+inc)%n; return (next<0) ? n+next : next; }