Newest File column

Overview

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

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

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

  • Newest File: Date, time and name.
  • Newest File Date: Date only.
  • Newest File Date-Time: Date and time.
  • Newest File Time: Time only.
  • Newest File Name: Name only.

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

  • Newest File (Recursive): Date, time and name.
  • Newest File Date (Recursive): Date only.
  • Newest File Date-Time (Recursive): Date and time.
  • Newest File Time (Recursive): Time only.
  • Newest File 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, but then you have two columns instead of one. 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 (Opus 13 and above):

Installation (Opus 12 and bellow):

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

History

  • v1.4 (30/Sep/2024)
    • Fixed error with empty folders when IncludeFolders was on.
  • v1.3 (29/Sep/2024)
    • Added config option to show data for individual files (on by default).
    • Added config option to consider (sub)-folder dates when calculating the newest date (off by default).
  • v1.2 (Not published until 1.3)
    • Added time-only versions of the columns.
  • 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.

Configuration

  • Go to the list of scripts.
    Opus 13 & above: Settings > Scripts.
    Opus 12 & below: Preferences / Toolbars / Scripts.
    You can also run Prefs SCRIPTS to open the appropriate UI in either version.

  • Configure the Newest File Column script:

Configuration options are:

  • IncludeFolders: False by default. When false, and calculating the column for a folder, the dates of the folder itself and all sub-folders are ignored; only file dates are considered. When true, if the folder itself or one of the sub-folders has the newest date, it will be what the column shows.

  • SingleFiles: True by default. When false, the column will be blank for individual files. When true, the column will show the date, name, etc. of the file (treating individual files as if they were folders that only contain themselves).

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 downloads above are easier.

The script code is reproduced here to help people looking for scripting techniques on the forum.

This script is in JScript.

// Newest File Column
// (C) 2014-2024 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-2024 Leo Davidson";
	initData.url = "https://resource.dopus.com/t/custom-column-newest-file/18765";
	initData.version = "1.4";
	initData.default_enable = true;
	initData.min_version = "11.5.2"

	initData.config.IncludeFolders = false;
	initData.config.SingleFiles = true;

	initData.config_desc = DOpus.Create.Map(
		"IncludeFolders", "Whether (sub-)folder dates are considered, or only files, when finding the newest item.",
		"SingleFiles", "If true, the column shows the modified date of individual files. If false, it column is blank for files.");

	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-Time";
	col.header = "Newest Date-Time";
	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-Time (Recursive)";
	col.header = "Newest Date-Time (Rec)"
	col.justify = "right";
	col.autogroup = true;
	col.type = "datetime";
	col.multicol = true;

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

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

	var col = initData.AddColumn();
	col.name = "NewestFileTime";
	col.method = "OnNewestFile";
	col.label = "Newest File Time";
	col.header = "Newest Time";
	col.justify = "right";
	col.autogroup = true;
	col.type = "time";
	col.multicol = true;

	var col = initData.AddColumn();
	col.name = "NewestFileTimeRec";
	col.method = "OnNewestFileRec";
	col.label = "Newest File Time (Recursive)";
	col.header = "Newest Time (Rec)"
	col.justify = "right";
	col.autogroup = true;
	col.type = "time";
	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)
{
	var SingleFiles    = Script.config.SingleFiles;
	var IncludeFolders = Script.config.IncludeFolders;

	var NewestItem = null;

	if (scriptColData.item.is_dir)
	{
		if (IncludeFolders)
		{
			NewestItem = scriptColData.item;
		}

		var FolderEnum = DOpus.FSUtil.ReadDir(scriptColData.item, IsRecursive);

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

			if (IncludeFolders || !FolderItem.is_dir)
			{
				if (NewestItem == null || NewestItem.modify.Compare(FolderItem.modify) < 0)
				{
					NewestItem = FolderItem;
				}
			}
		}
				
	}
	else if (SingleFiles)
	{
		NewestItem = scriptColData.item;
	}

	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("NewestFileDateOnly" + isRec))
		{
			scriptColData.columns("NewestFileDateOnly" + isRec).value = NewestItem.modify;
		}

		if (scriptColData.columns.exists("NewestFileTime" + isRec))
		{
			scriptColData.columns("NewestFileTime" + 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.

1 Like

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.

The "Newest file column" in this script only shows the modification time of the folder, which means I can only sort the folders. Iā€™d love to sort both folders and files together by their latest modification time. It would be perfect if the "Newest file column" could display the modification time of files as well. Is it possible to make this adjustment? Thanks a lot!

Root post has been updated with a new version of the script, with these changes:

  • v1.3 (29/Sep/2024)
    • Added config option to show data for individual files (on by default).
    • Added config option to consider (sub)-folder dates when calculating the newest date (off by default).
  • v1.2 (Not published until 1.3)
    • Added time-only versions of the columns.
2 Likes

Thank you for your quick update, much appreciated! I installed it, and it perfectly achieved the effect I wanted.:rose: