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