GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Column: AvgFileSize

column
vbscript
scripting

#1

Hi all. This is my first DOpus Add-In. It was born out of this thread.

Overview

This script creates a new column called AvgFileSize. It displays the average file size within a folder. It looks like this:


As you can see, it simply divides the folder size by the number of files it contains.

Installation

Download this file:
AvgFileSize.osp (1.03 KB)
Once downloaded, drag and drop the file into your DOpus Preferences dialog here:

Usage

Add the column to any lister like so:

Configuration

I gave the script an option to specify a filename filter. Only files that match the filter will be counted in the column.

It defaults to * to count all files. You could change it to *.jpg to count JPEG files only.

Source Code
The script code is reproduced here so that it can be searched through the forum.

' AvgFileSize Column
' © 2016 Waffles
'
' This is a script for Directory Opus 11. It creates a new column called "AvgFileSize". The column displays the average
' file size  within a folder. It only works on folders, not files.
'
' The script includes an option to allow the user to specify a filename filter. Only files that match the filter will be
' counted. It defaults to "*" to count all files.
'
' This script was created with the gracious help of Leo Davidson.

Option Explicit

Function OnInit(initData)
'Called by DOpus to initialize the script.

	With initData
	    .name = "AvgFileSize"
	    .desc = "Adds a script column that shows the average file size within a folder."
	    .copyright = "© 2016 Waffles"
	    .version = "1.0"
	    .default_enable = True
        .min_version = "11.17"
        .config.Filter = "*"
        .config_desc = DOpus.NewMap("Filter", "Standard DOpus filter to apply to the files counted by the AvgFileSize column.")
	End With

	With initData.AddColumn
    	.name = "AvgFileSize"
    	.header = "AvgFileSize"
    	.label = "AvgFileSize"
    	.type = "size"
    	.justify = "right"
    	.method = "OnAvgFileSize"
    End With

End Function

Function OnAvgFileSize(scriptColData)
'Called by DOpus to get the value of a column for an item.

    Dim lFileCount, FileSize, Wild, FolderEnum, FolderItem

    'Initialize tallies and get custom filter.
    lFileCount = 0
    Set FileSize = DOpus.FSUtil.NewFileSize
    Set Wild = DOpus.FSUtil.NewWild(Script.config.Filter)

    'If item is a folder, sum its files that match the filter recursively.
    If scriptColData.item.is_dir Then
        Set FolderEnum = DOpus.FSUtil.ReadDir(scriptColData.item, True) 'True=Recursive
        Do Until FolderEnum.complete
            Set FolderItem = FolderEnum.next
            If Not FolderItem.is_dir And Wild.Match(FolderItem.name) Then 'apply filter
                FileSize.Add(FolderItem.size)
                lFileCount = lFileCount + 1
            End If
        Loop
        If lFileCount = 0 Then lFileCount = 1 'avoid division by zero
        scriptColData.value = FileSize / lFileCount
    End if

End Function

Enjoy :grin:


AvgFileSize column script - can't get folder size
File Count
Folder size column format — ideal alignment
Columns: Maximum and Average File Size
#2

Nicely done! :slight_smile:


#3

Just what I was looking for!!

THANK YOU!


#4

Alternative version

Here's another script which provides maximum and average file size columns:

When I wrote the linked script, I didn't remember this script already existed, so there's some overlap.

I've linked to this script from the other script's thread as well, so people can pick the one which suits them the best. Also good to have examples in both scripting languages.