Item Count in Tab Name

Conducted both a help file and forum search, but may have missed the answer...

What methods/options exist to show number of items in folder tab in the Tab's name?

For example, Folder X has a subfolder and two files within it, for a total of 3 items. The tab name would display Folder X (3)

Please advise.

I guess you could do it via scripting but I think it would be quite a pain to keep the number updated and accurate (although it's not impossible to do).

In terms of what's built-in, only the status bar and filter bar display file counts.

Thanks for the response. From a workflow standpoint, it would be quicker for me to look at the tab rather than click on each tab to view the tab's status bar or filter bar.

Any scripts you could point me to that might demonstrate how to perform this task or at least get me started down the path?

Here's a little script to try (update #2).

function OnInit(initData) {
    initData.name = 'RenameTabItemCount';
    initData.version = '2022-12-30';
    initData.copyright = '';
    initData.url = 'https://resource.dopus.com/t/item-count-in-tab-name/43276';
    initData.desc = 'Show number of items in folder tab';
    initData.default_enable = true;
    initData.min_version = '12.0';
}

function OnOpenLister(openListerData) {
    DOpus.Output('*** OnOpenLister');
    if (!openListerData.after) return true;
    AppendItemCount(openListerData.lister.activetab);
    return false;
}

function OnActivateLister(activateListerData) {
    DOpus.Output('*** OnActivateLister');
    if (!activateListerData.active) return;
    AppendItemCount(activateListerData.lister.activetab);
}

function OnOpenTab(openTabData) {
    DOpus.Output('*** OnOpenTab');
    AppendItemCount(openTabData.tab);
}

function OnActivateTab(activateTabData) {
    DOpus.Output('*** OnActivateTab');
    AppendItemCount(activateTabData.newtab);
}

function OnSourceDestChange(sourceDestChangeData) {
    DOpus.Output('*** OnSourceDestChange');
    AppendItemCount(sourceDestChangeData.tab);
}

function OnAfterFolderChange(afterFolderChangeData) {
    DOpus.Output('*** OnAfterFolderChange');
    AppendItemCount(afterFolderChangeData.tab);
}

function OnDoubleClick(doubleClickData) {
    DOpus.Output('*** OnDoubleClick');
    AppendItemCount(doubleClickData.tab);
}

function OnDisplayModeChange(displayModeChangeData) {
    DOpus.Output('*** OnDisplayModeChange');
    AppendItemCount(displayModeChangeData.tab);
}

function OnTabClick(tabClickData) {
    DOpus.Output('*** OnTabClick');
    AppendItemCount(tabClickData.tab);
}

var cmd = DOpus.Create().Command();
var fsu = DOpus.FSUtil();

function AppendItemCount(tab) {
    for (var e = new Enumerator(tab.lister.tabs); !e.atEnd(); e.moveNext()) {
        var tabItem = e.item();

        DOpus.Output('tabItem.path: "' + tabItem.path + '"');

        var tabPath = String(tabItem.path);
        if (tabPath == '') continue;

        tabPath = fsu.Resolve(tabPath);

        var tabName = String(tabPath);

        if (tabName.substring(0, 2) == '::' || tabName.substring(0, 5) == 'coll:' || tabName.substring(0, 4) == 'ftp:') {
            tabName = fsu.DisplayName(tabPath);
        } else {
            if (tabName.slice(-1) == '\\') {
                var tabName = tabName.substring(0, 2);
            } else {
                var tabName = tabPath.filepart;
            }
        }

        var c = tabItem.all.count;
        var newTabName = tabName + (c > 0 ? (' (' + c + ')') : '');
        if (newTabName == tabItem.displayed_label) continue;

        cmd.SetSourceTab(tabItem);
        cmd.RunCommand('Go TABNAME="' + newTabName + '"');
    }
}

Save EventRenameTabItemCount.js.txt to

%appdata%\GPSoftware\Directory Opus\Script AddIns

https://resource.dopus.com/t/how-to-use-buttons-and-scripts-from-this-forum/3546

5 Likes

Perfect... THANK YOU!

Playing with the code, I wanted to eliminate the tab name change if the tab had no items, so changed the relevant code from this:

cmd.SetSourceTab(tabItem);
if (tabItem.all.count != 0) {
cmd.RunCommand('Go TABNAME="' + newTabName + '"');}

to this:

cmd.SetSourceTab(tabItem);
if (tabItem.all.count != 0) {
cmd.RunCommand('Go TABNAME="' + newTabName + '"');}

The script works very well with two exceptions:

  • when adding a new folder or file (requires tab refresh)
  • when deleting or moving a folder or file (the losing tab, especially Desktop, requires close then reopen)

As an aside, there is an "Invalid procedure call or argument (0x800a0005)" error at line 45 position 9:

var tabPath = fsu.Resolve(tabItem.path);

Otherwise, this works great. Thanks again!

I updated the script. Works now better with File Collections as well.

That's odd. I included a debug line in the script. Let me know which tabs cause trouble.

Thanks again for this. The error occurs when opening a lister that includes a Desktop tab. In addition, the Desktop tab causes two issues:

  1. Counts for the desktop tab only update when adding an item; removing items (move, delete) does not change the count.
  2. Starting in the Desktop tab, selecting another folder within the tab (e.g. File Collections), then moving back to the Desktop tab does NOT change the name of the tab back to Desktop. The name remains that of the previous folder (e.g. File Collections).

EDIT: Error occurs when opening a lister, regardless of which tabs are included. Could this be related to another script running on Lister open?

The script not detecting an update means we need to find the event that get triggered by the action (if there is such an event) and add it to the list of events in the script. I'll have a look.

Could be. I never had such an error, so there is hope :slight_smile:

To detect folder changes in a script, you'd need to use a Dialog and the WatchTab method. (The dialog's opacity can be set to zero so it's never visible.)

1 Like

Follow Up: I disabled all other scripts and restarted DOpus via dopusrt.exe /restart. Even with only the EventRenameTabItemCount script enabled, the error persists regardless of lister configuration or tabs displayed.

The OnOpenLister and OnOpenTab events sometimes send empty path objects, that cause these errors. I have updated the script to skip these tabs. FTP tabs will now look nicer, too.

I have added debug info to the script output. They can easily be suppressed by prefixing all DOpus.Output statements with //.

3 Likes

Works like a champ, thanks!

2lxp

Can you tell me a way to modify the code to display only the number of files or only the number of subfolders? TIA

Replace

var c = tabItem.all.count;

with

var c = tabItem.files.count;

or

var c = tabItem.dirs.count;
1 Like

2lxp
Much appreciated!

Revisiting this based on a recent quirk I noticed: hidden items (I'm sure the same issue exists for system items)

When in a tab with hidden items, the tab provides the correct number of items displayed. Clicking the "Hidden Files" link in the status bar displays the hidden files, but does not update the item count in the tab name unless one explicitly refreshes the tab. The same issue applies when hidden files are displayed and one clicks "Everything" in the status bar.

What object/event/method might we use to refresh the tab item count when displaying or hiding hidden files?

Hi @lxp was that detail fixed? Why do I have your second version of the script and I have to update the tab when I create new files/folders, or is it a wrong behavior of my PC? Thank you

I am afraid there is no event that gets triggered in that case.

No. Why not?