Column listing age of file in days - need script help

Don't feel pushed in any way, if you need to cut off time from family, please don't! o)

[quote="tbone"]Good! o)
What's left is setting the "sort" property for the new columns, as the items do not sort properly (because of mixed units of measurement and number of digits).
I'd also suggest to create two new script config items, to let the user choose the desired format for the columns. I'd like to see something like 00:00:12:31:00 e.g., which would mean 0 years, 0 days, 12hours etc. That could be configured like so: "%yy%:%dd%:%hh%:%mm%:%ss%" and the format you chose would look something like "%ageAuto% %unitAuto%". What do you think?

This is a function I used somewhere else, it features days already.
Years and the %auto% things are missing, but should be fairly easy to add, would you like to do that? o)

function SecondsToTime( s, f) { // seconds, format var d = h = m = 0; switch (true) { case (s > 86400): d = Math.floor(s / 86400); s %= 86400; case (s > 3600): h = Math.floor(s / 3600); s %= 3600; case (s > 60): m = Math.floor(s / 60); s %= 60; } if (f != null) { var f = f.replace('%dd%', (d < 10) ? "0" + d : d); f = f.replace('%d%', d); f = f.replace('%hh%', (h < 10) ? "0" + h : h); f = f.replace('%h%', h); f = f.replace('%mm%', (m < 10) ? "0" + m : m); f = f.replace('%m%', m); f = f.replace('%ss%', (s < 10) ? "0" + s : s); f = f.replace('%s%', s); } else { f = d + ':' + h + ':' + m + ':' + s; } return f; }[/quote]

So I'm looking at this and have added the year stuff. Not sure if it's correct or not:

function SecondsToTime( s, f) { // seconds, format var y = d = h = m = 0; switch (true) { case (s > 31557600): y = Math.floor(s / 31557600); s %= 31557600; case (s > 86400): d = Math.floor(s / 86400); s %= 86400; case (s > 3600): h = Math.floor(s / 3600); s %= 3600; case (s > 60): m = Math.floor(s / 60); s %= 60; } if (f != null) { var f = f.replace('%yy%', (y < 10) ? "0" + y : y); f = f.replace('%y%', y); f = f.replace('%dd%', (d < 10) ? "0" + d : d); f = f.replace('%d%', d); f = f.replace('%hh%', (h < 10) ? "0" + h : h); f = f.replace('%h%', h); f = f.replace('%mm%', (m < 10) ? "0" + m : m); f = f.replace('%m%', m); f = f.replace('%ss%', (s < 10) ? "0" + s : s); f = f.replace('%s%', s); } else { f = y + ':' + d + ':' + h + ':' + m + ':' + s; } return f; }
I am not sure how to do the %auto% portion, nor how to incorporate this into a sorting mechanism.

Looking at the sort issue, one would think we could simply use the calculated diffMin variable like this:

data.columns("ModifiedWithin-Chars").sort = diffMin;

However, it doesn't work. What am I missing?

Is that in place of the line which currently does this?

data.columns("ModifiedWithin-Chars").sort = Script.config["CharsColumn.MaxLength"]-value.length;

What are some example diffMin values? It might make sense to output them to the scripting log so you can see what you are putting into the object.

Yes

A great script, by the way. :thumbsup:

[quote="leo"]Is that in place of the line which currently does this?

data.columns("ModifiedWithin-Chars").sort = Script.config["CharsColumn.MaxLength"]-value.length;

What are some example diffMin values? It might make sense to output them to the scripting log so you can see what you are putting into the object.[/quote]

I thought that was the line, but I was mistaken. I actually figured out how to appropriately sort. I'll make some more updates and upload an updated version.

It's all tbone. I'm just a hack! :laughing:

Great work guys :thumbsup:

Is there a final version somewhere ?

I know it's been a while (ok a long while!)...

Finalizing this, but have run into a problem: I can't make the column configs function independently. Any help would be appreciated!
Column.Generic_Age.js.txt (14.8 KB)

For those who just want to see the code:

[code]//v1.0 - 27 Nov 2016
//- initial release based on tbone's ModifiedWithin column script

//==================================================
function OnInit(data) {
//uid added via script wizard (do not change after publishing this script)
var uid = "4CBC870D-C8E5-435F-8F68-FA1AA7E16DBB";
//resource center url added via script wizard (required for updating)
var url = ""
data.name = "Column.Generic: Age"
data.desc = "Adds sortable columns that indicate folder/file age since creation or modification"
data.copyright = "Chuck"
data.url = "Column listing age of file in days - need script help
data.version = "1.0"
data.min_version = "11.7.1"
data.default_enable = true;
//==================================================
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("Created", DOpus.Create.Vector()).
des("Auto lists time created as ## [years|days|hrs|mins].\nUnits lists time created as yy:dd:hh:mm:ss").
val(0).val("Auto").val("Units");
cfg.add("Modified", DOpus.Create.Vector()).
des("Auto lists time modified as ## [years|days|hrs|mins].\nUnits lists time modified as yy:dd:hh:mm:ss").
val(0).val("Auto").val("Units");
}

//==================================================
function OnAddColumns(data){
var cmd = data.AddColumn();
cmd.multicol = true;
cmd.name = "DaysSinceCreation";
cmd.label = "Days Since Creation";
cmd.header = "Age (C)";
var method = ["Auto","Units"];
cmd.method = method[Script.config["Created"]];
cmd.autorefresh = true;
cmd.namerefresh = true;
cmd.defwidth = "60px";
cmd.type = "text";
cmd.justify = "right";

var cmd = data.AddColumn();
cmd.multicol = true;
cmd.name = "DaysSinceModified";
cmd.label = "Days Since Modification";
cmd.header = "Age (M)";
var method = ["Auto","Units"];
cmd.method = method[Script.config["Modified"]];
cmd.autorefresh = true;
cmd.namerefresh = true;
cmd.defwidth = "60px";
cmd.type = "text";
cmd.justify = "right";

}
//==================================================

function Auto(data) {
var dateMod = new Date(data.item.modify).valueOf();
var dateCre = new Date(data.item.create).valueOf();
var dateNow = new Date().valueOf();
var diffCre = (dateNow-dateCre)/60000;
var diffMod = (dateNow-dateMod)/60000;

//setting defaults (no value to clutter columns)
data.columns("DaysSinceCreation".value) = "";
data.columns("DaysSinceModified".value) = "";

//Determine age in minutes since file/folder created and add appropriate suffix to calculation
if (diffCre < 60){
	data.columns("DaysSinceCreation").value = (diffCre).toFixed(0) + " min";
}	else if (diffCre < 1440){
		data.columns("DaysSinceCreation").value = ((diffCre)/60).toFixed(1) + " hrs";
}   else if (diffCre < 525960){
		data.columns("DaysSinceCreation").value = ((diffCre)/1440).toFixed(1) + " days";
}	
	else {
		data.columns("DaysSinceCreation").value = ((diffCre)/525960).toFixed(1) + " yrs";
}
data.columns("DaysSinceCreation").sort = diffCre;

//Determine age in minutes since file/folder modified and add appropriate suffix to calculation
if (diffMod < 60){
	data.columns("DaysSinceModified").value = (diffMod).toFixed(0) + " min";
}	else if (diffMod < 1440){
		data.columns("DaysSinceModified").value = ((diffMod)/60).toFixed(1) + " hrs";
}   else if (diffMod < 525960){
		data.columns("DaysSinceModified").value = ((diffMod)/1440).toFixed(1) + " days";
}	
	else {
		data.columns("DaysSinceModified").value = ((diffMod)/525960).toFixed(1) + " yrs";
}
data.columns("DaysSinceModified").sort = diffMod;

}
//==================================================

function Units(data) {
var dateMod = new Date(data.item.modify).valueOf();
var dateCre = new Date(data.item.create).valueOf();
var dateNow = new Date().valueOf();
var diffCre = (dateNow-dateCre)/60000;
var diffMod = (dateNow-dateMod)/60000;
var y = d = h = m = sc = sm = 0;

//setting defaults (no value to clutter columns)
data.columns("DaysSinceCreation".value) = "";
data.columns("DaysSinceModified".value) = "";

//Determine age since file/folder created and add appropriate suffix to calculation
sc = (dateNow-dateCre)/1000;
switch (true) {
case (sc > 31557600):
	y = Math.floor(sc / 31557600);
	sc %= 31557600;
case (sc > 86400):
	d = Math.floor(sc / 86400);
	sc %= 86400;
case (sc > 3600):
	h = Math.floor(sc / 3600);
	sc %= 3600;
case (s > 60):
	m = Math.floor(sc / 60);
	sc %= 60;
	sc = sc.toFixed(0);
}

//Determine age since file/folder modified and add appropriate suffix to calculation
sm = (dateNow-dateMod)/1000;
switch (true) {
case (sm > 31557600):
	y = Math.floor(sm / 31557600);
	sm %= 31557600;
case (sm > 86400):
	d = Math.floor(sm / 86400);
	sm %= 86400;
case (sm > 3600):
	h = Math.floor(sm / 3600);
	sm %= 3600;
case (sm > 60):
	m = Math.floor(sm / 60);
	sm %= 60;
	sm = sm.toFixed(0);
}

//Correct numbers < 10 to add leading 0
var yy = y.toString();
var dd = d.toString();
var hh = h.toString();
var mm = m.toString();
var ssc = sc.toString();
var ssm = sm.toString();

yy = yy.replace(yy, (y < 10) ? "0" + y : y);
dd = dd.replace(dd, (d < 10) ? "0" + d : d);
hh = hh.replace(hh, (h < 10) ? "0" + h : h);
mm = mm.replace(mm, (m < 10) ? "0" + m : m);
ssc = ssc.replace(ssc, (sc < 10) ? "0" + sc : sc);
ssm = ssm.replace(ssm, (sm < 10) ? "0" + sm : sm);

//Format columns to display
var fc = yy + ':' + dd + ':' + hh + ':' + mm + ':' + ssc;
data.columns("DaysSinceCreation").value = fc;
data.columns("DaysSinceCreation").sort = diffCre;

var fm = yy + ':' + dd + ':' + hh + ':' + mm + ':' + ssm;
data.columns("DaysSinceModified").value = fm;
data.columns("DaysSinceModified").sort = diffMod;

}
//==================================================

function OnScriptConfigChange(data) {
Script.InitColumns();
Script.RefreshColumn("DaysSinceCreation");
Script.RefreshColumn("DaysSinceModified");
}
///////////////////////////////////////////////////////////////////////////////
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('Command: ScriptWizard (multipurpose scripting resources aid));}
else
cmd.RunCommand('ScriptWizard ABOUT WIN='+data.window+' FILE="'+Script.File+'"');
}
//MD5 = "072d9ba86ac35f55d8593a417ec4906b"; DATE = "2016.11.27 - 08:37:35"
[/code]

For those scripting experts out there, I would really appreciate your help. Specifically, the columns won't format independently of one another:

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("Created", DOpus.Create.Vector()). des("Auto lists time created as ## [years|days|hrs|mins].\nUnits lists time created as yy:dd:hh:mm:ss"). val(0).val("Auto").val("Units"); cfg.add("Modified", DOpus.Create.Vector()). des("Auto lists time modified as ## [years|days|hrs|mins].\nUnits lists time modified as yy:dd:hh:mm:ss"). val(0).val("Auto").val("Units");
The full code is listed in my prior entry. Again, I appreciate any help you might offer.

Format in which sense? I'm not sure I understand the questiom, sorry. Can you give more details, and maybe some examples of what happens vs what you want to happen?

Leo,

The configuration for each column do not work as expected or intended when the config is different for each column. If both are set to "auto," the columns appear as expected:



If both are set to "Units," the columns appear as expected:


However, when both are set differently, they default to the Age (C) config:




I'm guessing it's because the "Created" config comes first in the script. I would like for the columns to be independently configured, if possible.

So what am I missing?

Thanks for taking a look!

Is the last full snippet still the one you are using?

I spotted a weird thing in OnAddColumns():

.. var method = ["Auto","Units"]; cmd.method = method[Script.config["Modified"]]; ..
I get the idea behind, but if this is still the way you do it, it's actually surprising this works at all.
You should remove the dquotes around "Auto" and "Units", since you want the functions to be passed and not strings containing their names.

After changing the script config it might also be necessary to use the OnAfterScriptConfigChange() event or what it's exact name is.
In that call Script.InitColumns() to rebuild them with the updated function reference in cmd.method = .. I might be wrong and if that does not cure your issues, consider uploading a current version to play with. Thx! o)

Taking out the dquotes totally kills the script. I have included it for your consideration.
Column.Generic_Age_Working.js.txt (7.83 KB)

Yes, sorry, forget what I said about the dquotes. I mixed things up with the j(ava)script I do elsewhere currently. The column.method property expects a string with a function name of course, not a reference to a function (which would be just the name of the function without the dquotes).

There error is somehwere else and it is not in the way you assign functions for the columns. It's in the column functions, as they always set both columns. That's why you cannot have one column showing units, while the other is set to auto.

I removed some of the duplicate code which served both columns at the same time.
The column function now only act on one column at a time, so using different methods for each makes sense eventually.
Column.Generic_Age_Working.js.txt (11.8 KB)
While I tinkered with that script I was also able to reproduce a script column bug I saw with another script. So if the uploaded fix works for you, I guess we have a win win situation here. o)

Thanks for the help tbone.

Unfortunately, your adjustment didn't solve the issue, although it changed it a bit. Regardless of config (same or different), only the first column opened will display info based on its specific setting. The other column, when opened, will appear blank. With both columns displayed, Refresh All will display Age (C) in its specific config and Age (M) blank.



It just doesn't seem to want to work through the functions independently. Not sure what's missing...

Watch this, it seems you're also affected by the bug I was talking about: resource.dopus.com/viewtopic.ph ... 96#p154696

tracking!