Another jscript - file count appended to folder name

You select a directory and run the script.

It counts the files in the selected directory and appends that count at the end of folder name.

My use case: I needed this because there are multiple song mixing versions and we wanted the track count easily seen in the folder name for a quicker identification of the mix complexity.

With over 500 songs w/ all versions doing this manually wasn't going to happen soon.

John Smith - Happy Birthday (34 tracks)

The same concept could be used to put anything in the folder name once its selected.

Thought I would share:

function OnClick(clickData) {
    var tab = clickData.func.sourcetab;
    var sel = tab.selected;

    for (var e = new Enumerator(sel); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        if (!item.is_dir) continue;

        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var folder = fso.GetFolder(item.realpath);

        var count = 0;
        var files = new Enumerator(folder.Files);
        for (; !files.atEnd(); files.moveNext()) count++;

        var originalPath = item.realpath;
        var parentPath = fso.GetParentFolderName(originalPath);
        var baseName = item.name.replace(/\s\(\d+\stracks\)$/i, ""); // Remove any previous suffix
        var newName = baseName + " (" + count + " tracks)";
        var newPath = fso.BuildPath(parentPath, newName);

        // Rename using internal command
        clickData.func.command.ClearFiles();
        clickData.func.command.AddFile(item);
        clickData.func.command.RunCommand('Rename FROM="' + originalPath + '" TO="' + newPath + '"');
    }
}

Opus 13 provides the file count directly:

* {=filecount=}
1 Like

A similarly easy way to remove the suffix?

You be the judge :slight_smile:

 \d+$

A deluxe toggle version required a script in Opus 12, but should nowadays be doable with the evaluator.

That's great, but in this case, I'm sharing these in an online company cloud, where users don't have access to Directory Opus, so it has to be embedded in the folder name. That also requested by the client. Good information to know for my stuff. Thanks.

The screenshot shows the Advanced Rename dialog, not the file display.

@lxp

Thank you very much for the regex. A thing is simple when you see it and invisible when you don't.

This issue is solved.