Newest File column

Overview

This script provides columns which show the name and date of the newest file below each folder.

The script adds six columns. You'll only use one or two at a time.

Three of the columns report on files directly below each folder, ignoring sub-folders:

  • Newest File: Date and name.
  • Newest Date: Date only.
  • Newest Name: Name only.

The other columns report on files anywhere below each folder, including sub-folders:

  • Newest File (Recursive): Date and name.
  • Newest Date (Recursive): Date only.
  • Newest Name (Recursive): Name only.

The combined date and name columns let you turn on just one column to see both things, but the information is harder to read because the dates, times and filenames do not line up nicely. This isn't the case with the separate date and name columns. It's up to you which you use.

Sorting and grouping:

  • The combined columns sort by date/time and do not currently support grouping.
  • The date columns sort by date/time, and groups like the internal date columns do.
  • The name columns sort by name (alphabetically), and do not currently support grouping.

These columns read the list of files below each folder every time the file display is refreshed. For the recursive ones, that includes listing all sub-folders, and if you turn them on at the root of a drive the script will have to list every file on the drive. With large, complex directory trees, it can take a few seconds (and lots of disk/network activity) for the information to appear. You probably don't want to turn these columns on all the time, or in folders like C:. Use them when you need them and turn them off afterwards.

Installation

  • Download Newest File Column.js.txt (3.5 KB) v1.1
  • Open Preferences / Toolbars / Scripts to display the list of scripts.
  • Drag the downloaded Newest File Column.js.txt to the list.

History

  • v1.1 (15/Apr/2017)
    • "Subscript out of range" errors should be fixed now. They were due to this.
  • v1.0 (2014)
    • Initial version.

Usage

If the script is installed, you will see its columns under the Scripts category in the Folder Options dialog, or the menu you get from right-clicking your existing columns.

Add the columns manually as you would any others.

Alternatively, you might want to use a command, like this, which toggles a pair of the columns at once:

@ifset:COLUMNSTOGGLE="scp:Newest File Column/NewestFileDate"
Set COLUMNSREMOVE="scp:Newest File Column/NewestFileDate"
Set COLUMNSREMOVE="scp:Newest File Column/NewestFileName"
@ifset:else
Set COLUMNSADD="scp:Newest File Column/NewestFileDate"
Set COLUMNSADD="scp:Newest File Column/NewestFileName"

The command editor's menus will work out the column names for you, so don't worry about doing that by hand.

The Script Itself

Expand the section below if you want to look at the script to see how it works.

The script

If you just want to use the script, the Newest_File_Column.js.txt download above is probably easier.

The script code is reproduced here so that people looking for scripting techniques on the forum can browse the script code without having to download and open the .js.txt file.

This script is in JScript.

// Newest File Column
// (C) 2014-2017 Leo Davidson
// 
// This is a script for Directory Opus.
// See http://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.

function OnInit(initData)
{
	initData.name = "Newest File Column";
	initData.desc = "Columns to show the newest file below each folder";
	initData.copyright = "(C) 2014-2017 Leo Davidson";
	initData.url = "https://resource.dopus.com/t/custom-column-newest-file/18765";
	initData.version = "1.1";
	initData.default_enable = true;
	initData.min_version = "11.5.2"

	var col = initData.AddColumn();
	col.name = "NewestFile";
	col.method = "OnNewestFile";
	col.label = "Newest File";
//	col.header = "Newest File";
	col.justify = "left";
	col.autogroup = true;
	col.multicol = true;

	var col = initData.AddColumn();
	col.name = "NewestFileRec";
	col.method = "OnNewestFileRec";
	col.label = "Newest File (Recursive)";
	col.header = "Newest File (Rec)";
	col.justify = "left";
	col.autogroup = true;
	col.multicol = true;

	var col = initData.AddColumn();
	col.name = "NewestFileDate";
	col.method = "OnNewestFile";
	col.label = "Newest File Date";
	col.header = "Newest Date";
	col.justify = "right";
	col.autogroup = true;
	col.type = "datetime";
	col.multicol = true;

	var col = initData.AddColumn();
	col.name = "NewestFileDateRec";
	col.method = "OnNewestFileRec";
	col.label = "Newest File Date (Recursive)";
	col.header = "Newest Date (Rec)"
	col.justify = "right";
	col.autogroup = true;
	col.type = "datetime";
	col.multicol = true;

	var col = initData.AddColumn();
	col.name = "NewestFileName";
	col.method = "OnNewestFile";
	col.label = "Newest File Name";
	col.header = "Newest Name"
	col.justify = "left";
	col.autogroup = true;
	col.multicol = true;

	var col = initData.AddColumn();
	col.name = "NewestFileNameRec";
	col.method = "OnNewestFileRec";
	col.label = "Newest File Name (Recursive)";
	col.header = "Newest Name (Rec)"
	col.justify = "left";
	col.autogroup = true;
	col.multicol = true;
}


function OnNewestFile(scriptColData)
{
	NewestFileMain(scriptColData, false);
}

function OnNewestFileRec(scriptColData)
{
	NewestFileMain(scriptColData, true);
}

function BestItemName(item)
{
	if (item.display_name != "")
		return item.display_name;
	return item.name;
}

function NewestFileMain(scriptColData, IsRecursive)
{
	if (scriptColData.item.is_dir)
	{
		FolderEnum = DOpus.FSUtil.ReadDir(scriptColData.item, IsRecursive);

		NewestItem = null;

		while (!FolderEnum.complete)
		{
			FolderItem = FolderEnum.next;

			if (!FolderItem.is_dir)
			{
				if (NewestItem == null || NewestItem.modify.Compare(FolderItem.modify) < 0)
				{
					NewestItem = FolderItem;
				}
			}
		}

		if (NewestItem != null)
		{
			var isRec = (IsRecursive ? "Rec" : "");

			if (scriptColData.columns.exists("NewestFileName" + isRec))
			{
				scriptColData.columns("NewestFileName" + isRec).value = BestItemName(NewestItem) + "";
			}

			if (scriptColData.columns.exists("NewestFileDate" + isRec))
			{
				scriptColData.columns("NewestFileDate" + isRec).value = NewestItem.modify;
			}

			if (scriptColData.columns.exists("NewestFile" + isRec))
			{
				DateVar = new Date(NewestItem.modify);
				scriptColData.columns("NewestFile" + isRec).value = NewestItem.modify.Format() + " " + BestItemName(NewestItem);
				scriptColData.columns("NewestFile" + isRec).sort = DateVar.toJSON() + " " + BestItemName(NewestItem);
			}
		}
	}
}

Alternative:

A similar but slightly different script can be found here:

4 Likes

Thanks Leo. :thumbsup:

This is a very useful addition to the ever growing arsenal of Opus goodies.

Regards, AB

It can't be changed to Chinese character, bad luck.

What can't be?

Such as, "Newest File Column", "Newest File". If replaced by Chinese character, The script will go wrong.

It works fine. Make sure you save the script as UTF-8 with a BOM so the non-ASCII characters are saved correctly.

Notepad does the correct thing if you use the Encoding drop-down while saving.


I notice that errors occur on first use after Opus has started. I am using original @Leo script code and button code downloaded from the head post. No errors occur on second and subsequent executions. I had a look at the code and can't see anything untoward.

I think that should be fixed now. Updated script in the root post. Please give it a try.

I want to label the newest file with a filter in current directory, it is very useful too.

While adding those new columns is pretty straight forward, I would like to assign a label to a folder when the content of the column "Newest Date (rec)" is within one week.

I've already set up a similar label assignement, however with those new date columns I cannot use a certain interval on which the line gets labelled.

E.g.
2020-02-02_133749

Guess this is not possible, correct?

Hello Micky and Leo, I am still finding the same function as your advice, and this was just like the function that almost finished the method for tracing to and colorizing each folder in the path of the modified files within a time.
I am very new to the sript, and I tried to replace the col.type from "datetime" into "date", but still fail to find the words "within a period".
Wish master @Leo could see this and it could be really really helpful if could advise some other ways to get it done. And Micky bro please tell me if you find another way to get the target.
Many Thanks.

Using this for labels, unless the labels are only enabled in specific, shallow folders, would cause a lot of extra disk/ssd access. It would also take some time to calculate, so the color would not change until some time after the folder was displayed.

Both would be no worse than how long it takes to display the column itself, but the column is something you'd only turn on when you need it, and not have turned on all the time. Labels tend to be on all the time (unless you're going to define it inside a Folder Format, only for a specific location).

Is it something you really need as a label?

Really thanks to your quick reply.
I actually want the function is that I wish folder could be colorized(labeled) by 2 conditions:

  1. if the folder modified date is within 1 week;
  2. if the any subsets(subfiles or subfolders or sub-subfiles) of the folder are modified date is within 1 week , I also want the folder colorized.

Maybe to sum up, I want to get the effect just like the highlight a folder tree / path tree of each modified files in a period time.

I do not really know if it could be resolved or not, or there could be another way to get it?

for example, I want the highlighted marked folder "4" and "4.1" could be labeled when checking the modified files is on label.

image

I wouldn't advise applying that kind of label to the folder tree. It'll result in so much extra disk access, since the folders have to be read (recursively) to find out which file is the newest, and the tree potentially shows a huge number of folders, including ones you aren't actively working on, network drives, and so on.

I'd go a with static solution, i.e. a script that colors the folders. The coloration will be correct for 24 hours (or until midnight, depending on your definition of a week). Then the script needs to run again. A bit like applying make-up :wink:

1 Like

Great appreciation for the explanation again.
Well, It is time to quit to find the method to the impossible work.

It sounds like the other way to get to the Rome. Thanks for your advice.