GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Columns: Maximum and Average File Size

column
scripting
jscript

#1

Overview

This script provides columns which show the maximum and average file sizes below each folder.

Recursive and non-recursive versions of each column are provided.

The average size calculation can ignore small files, so extra files do not skew the average you're interested in. (e.g. If you want to know the average size of mp3 files in your album folders, then you won't want the tiny .m3u playlists and .jpg cover-art files to be considered.) The minimum size can be configured via the script's config dialog. If no files meet the minimum then the average of all files will be returned for that folder.

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'll probably only want to turn these columns on for particular folders, or to turn them on and off as needed for special situations.

Installation

  • Download Max Size Column.js.txt (4.4 KB) v1.0
  • Open Preferences / Toolbars / Scripts to display the list of scripts.
  • Drag the downloaded Max Size Column.js.txt to the list.

History

  • v1.0 (15/Apr/2017)
    • Initial version.

Usage

Usage is similar to other scripts which add columns. To avoid repetition and keep the post small, see the Custom Column - Newest File script if you need some examples.

Configuration

To configure the script, go to Preferences / Toolbars / Scripts and click its name, which should be underlined to indicate is is configurable.

A window will open where you can set the following:

  • AvgMinSize (default: 5 MB) - Files smaller than this will be ignored when calculating average sizes, unless no files large enough exist in the folder. See the Overview section above for more discussion.

The Script Itself

If you just want to use the script, the download above is 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.

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

var avgTestSize = null;

function OnInit(initData)
{
	initData.name = "Max Size Column";
	initData.desc = "Columns to show max size and average size below folders";
	initData.copyright = "(C) 2017 Leo Davidson";
	initData.url = "https://resource.dopus.com/t/column-maximum-and-average-file-size/25572";
	initData.version = "1.0";
	initData.default_enable = true;
	initData.min_version = "12.4"

	initData.config.AvgMinSize = "5 MB";
	initData.config_desc = DOpus.Create.Map("AvgMinSize", "When calculating average sizes, ignore files smaller than this unless they are the only ones. Bytes unless KB, MB or GB suffix added.")
}

function updateAvgTestSize()
{
	avgTestSize = DOpus.FSUtil.NewFileSize(0);

	var sizeCfg = Script.config.AvgMinSize;
	var units = "";

	if (typeof(sizeCfg) === "string")
	{
		var matches = sizeCfg.match( /^\s*(\d+)\s*(\S*)\s*$/i );
		if (matches !== null && matches.length > 1)
		{
			sizeCfg = parseInt(matches[1]);
			if (matches.length > 2)
			{
				units = matches[2].toUpperCase();
			}
		}
	}

	if (typeof(sizeCfg) === "number")
	{
		avgTestSize.Set(sizeCfg);
		switch(units)
		{
			default:
				DOpus.Output("Max Size Column's AvgMinSize config value has invalid units.", true);
				avgTestSize.Set(0);
				break;
			case "GB":
				avgTestSize.Mult(1024);
			case "MB":
				avgTestSize.Mult(1024);
			case "KB":
				avgTestSize.Mult(1024);
			case "BYTES":
			case "":
				break;
			
		}
	}
	else
	{
		DOpus.Output("Max Size Column's AvgMinSize config value is invalid.", true);
		avgTestSize.Set(0);
	}
}

function OnScriptConfigChange(configChangeData)
{
	updateAvgTestSize();
}

function OnAddColumns(addColData)
{
	updateAvgTestSize();

	var col = addColData.AddColumn();
	col.name = "MaxSize";
	col.method = "OnColumnsShallow";
	col.label = "Max Size";
//	col.header = "Max Size";
	col.type  = "size";
	col.justify = "right";
	col.autogroup = true;
	col.multicol = true;

	col = addColData.AddColumn();
	col.name = "MaxSizeRec";
	col.method = "OnColumnsRecursive";
	col.label = "Max Size (Recursive)";
	col.header = "Max Size (Rec)";
	col.type  = "size";
	col.justify = "right";
	col.autogroup = true;
	col.multicol = true;

	var col = addColData.AddColumn();
	col.name = "AvgSize";
	col.method = "OnColumnsShallow";
	col.label = "Avg Size";
//	col.header = "Avg Size";
	col.type  = "size";
	col.justify = "right";
	col.autogroup = true;
	col.multicol = true;

	col = addColData.AddColumn();
	col.name = "AvgSizeRec";
	col.method = "OnColumnsRecursive";
	col.label = "Avg Size (Recursive)";
	col.header = "Avg Size (Rec)";
	col.type  = "size";
	col.justify = "right";
	col.autogroup = true;
	col.multicol = true;
}


function OnColumnsShallow(scriptColData)
{
	OnColumnsMain(scriptColData, false);
}

function OnColumnsRecursive(scriptColData)
{
	OnColumnsMain(scriptColData, true);
}

function OnColumnsMain(scriptColData, IsRecursive)
{
	if (scriptColData.item.is_dir)
	{
		if (avgTestSize === null)
		{
			updateAvgTestSize();
		}

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

		var maxSize = DOpus.FSUtil.NewFileSize(0);
		var avgTotalSize = DOpus.FSUtil.NewFileSize(0);
		var itemCount = 0;
		var avgTotalSizeSmall = DOpus.FSUtil.NewFileSize(0);
		var itemCountSmall = 0;

		while (!folderEnum.complete)
		{
			var folderItem = folderEnum.next;

			if (!folderItem.is_dir)
			{
				if (maxSize.Compare(folderItem.size) < 0)
				{
					maxSize.Set(folderItem.size);
				}
				
				if (avgTestSize.Compare(folderItem.size) <= 0)
				{
					avgTotalSize.Add(folderItem.size);
					++itemCount;
				}
				
				avgTotalSizeSmall.Add(folderItem.size);
				++itemCountSmall;
			}
		}

		if (itemCount > 0)
		{
			avgTotalSize.Div(itemCount);
		}
		else if (itemCountSmall > 0)
		{
			avgTotalSize.Set(avgTotalSizeSmall);
			avgTotalSize.Div(itemCountSmall);
		}

		var isRec = (IsRecursive ? "Rec" : "");
		var maxName = "MaxSize" + isRec;
		var avgName = "AvgSize" + isRec;

		if (scriptColData.columns.exists(maxName))
		{
			scriptColData.columns(maxName).value = maxSize;
		}

		if (scriptColData.columns.exists(avgName))
		{
			scriptColData.columns(avgName).value = avgTotalSize;
		}
	}
}

Alternative version

Here's another script which provides an average file size column:


Column: AvgFileSize
#2

Does this work in DO11? I see it in the Preferences/toolbars/scripts. But it's not in the Columns/Scripts menu when I try to add the column.

Thanks


#3

It was written a year after the last Opus 11 update, so it might depend on newer features added in Opus 12 since then.

initData.min_version = "12.4" in the script indicates the minimum version it has been tested with, and that can load it.