Column listing age of file in days - need script help

I am looking for a custom column that lists the age of any file in days, either based on the last modified date or date created. I know next to nothing about scripting, so any assistance (sample script, already built script to learn from) would be appreciated.

Cheers!

Chuck

These should get you started:

Column scripts in the manual

Column scripts in the scripting forum

Why don't you just use one of the columns provided by the "ModifiedWithin" column set? o)

Freshly updated right here: Column: ModifiedWithin (alternative to 'modified'-labels)
Adding the new columns just took two additional lines (despite the column bootstrapping), hope you can make some use of it. o)

[quote="tbone"]Why don't you just use one of the columns provided by the "ModifiedWithin" column set? o)

Freshly updated right here: Column: ModifiedWithin (alternative to 'modified'-labels)
Adding the new columns just took two additional lines (despite the column bootstrapping), hope you can make some use of it. o)[/quote]

Yours is the script I was using as an example; just hadn't muddled through yet. Thanks so much for your efforts tbone!

You're welcome. o)

In case you downloaded the updated version, consider downloading or updating it with ScriptWizard once again.
There was a tiny error in v0.2, which may lead to a parsing error/non-working script.

[quote="tbone"]You're welcome. o)

In case you downloaded the updated version, consider downloading or updating it with ScriptWizard once again.
There was a tiny error in v0.2, which may lead to a parsing error/non-working script.[/quote]

I actually made some tweaks to the script to automatically determine age in min, hrs, days and years. I've attached it for review.
Column.Generic_ModifiedWithin_3.js.txt (17 KB)

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; }

tbone,

I'll leave it to you as the expert here. That and work/family won't allow the time this week. I may be able to look this weekend, but no sooner.

Chuck

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.