Rename Folder, adding number of files within it?

I understand it may appear to be a bizarre request, but is there a script or some type of method to rename a folder that simply adds the number of subfiles its name? Example:
Music Folder / Files needed to Sort
rename the last folder to
Music Folder / Files needed to Sort (# of subfiles)

Any help or guidance would be appreciated. Thank you community!

This thread may help: Item Count in Tab Name

Here you go.

// https://resource.dopus.com/t/rename-script-inquiry-rename-folder-to-have-number-of-files-within-it-added-to-the-folder-name/45103

var fso = new ActiveXObject('Scripting.FileSystemObject');

function OnGetNewName(getNewNameData) {
    if (!getNewNameData.item.is_dir) return;
    return getNewNameData.newname + ' (' + fso.GetFolder(getNewNameData.item).Files.count + ')';
}

Save 45103.orp to

%appdata%\GPSoftware\Directory Opus\Rename Presets

How to use Rename Presets from this forum

1 Like

Dudes, thank you to both of you for your replies and assisting with my randomness.

But YOU, Mr. lxp, deserve a Distinguished Service Cross, or some other shiny metal object to denote absolutely fantastic service to this board, and showing poise and bravery in the line of bizarre questions and requests by others and myself.

4 Likes

2lxp

Script Update Request:

  1. Code to remove the item count
  2. Code such that when the script is run a second time it removes the count of the prior time it was run

May I suggest, separate scripts for these two. Then all three could be put into a three button.

Updated version that lets you toggle the file count.

// https://resource.dopus.com/t/rename-script-inquiry-rename-folder-to-have-number-of-files-within-it-added-to-the-folder-name/45103

// 2023-07-22

var fso = new ActiveXObject('Scripting.FileSystemObject');
var re = /(.*) \(\d+\)/;

function OnGetNewName(getNewNameData) {
    var item = getNewNameData.item;
    if (!item.is_dir) return;

    var tmp = re.exec(item.name);

    if (tmp != null && tmp.length == 2) {
        var newName = tmp[1];
    } else {
        var newName = item.name + ' (' + fso.GetFolder(item).Files.count + ')';
    }

    return newName;
}

45103b.orp (857 Bytes)

2 Likes

2lxp

Thank you so much for the updated script. Sometime ago you suggested to me that learning to write scripts would be beneficial. I am working on it. Thanks.

Would it possible to modify this script so that it works directly from a button? Thank you so much!

No need to modify the script, you can reference it directly in a button:

@nodeselect
Rename PRESET=45103b

https://resource.dopus.com/t/how-to-use-rename-presets-from-this-forum/26726#using-a-rename-preset-outside-the-rename-dialog-5

1 Like

True, but that way you would have to have the script on one side and a button on the other side, for me it would very helpful to have it all together in a single button...

function OnClick(clickData)
{
var fso = new ActiveXObject('Scripting.FileSystemObject');

function OnGetNewName(getNewNameData) {
    if (!getNewNameData.item.is_dir) return;
    return getNewNameData.newname + ' (' + fso.GetFolder(getNewNameData.item).Files.count + ')';
}
}

Man you exceeded my expectations for what I asked for by so much and I appreciate your expertise immensely! Thank you so much for contributing I can't tell you how much I rely on the kindness and Goodwill of everyone on this board so I really do appreciate your hard work

1 Like

I'm new to the forum, it's the first time I ask for help, and from what I see lxp is a very active user, he helps a lot, congratulations!

Is that a complete button code? I have tried this code in a button set the function of the button in scripting but it's doesn't work in my test. what's the wrong? Is that possible to count only .flac within the folder? Thanks

No @khalidhosain, that code that I posted doesn't work, it was am attempt by me to want to adapt the original lpx code to a button, but I didn't get it, sorry

1 Like

It's Ok I have get your solve. If you want to use the code just in a single button like that


Set the button function in Standard Function (Opus or external)
then add this code before lxp's code

@nodeselect 
Rename TO "*"
@script Jscript

Enjoy!!

But here comes my Problem. I want to count Only the Music files I mean want to use
If File extension is (.flac|.mp3|.m4a|.wav)
Please some one help with some line of code. Thanks in advance.

Thank you very, very much @khalidhosain, great help!!!

1 Like

Will it be very difficult to make the script also count the folders, that is, files + folders, as it is, in a non-recursive way? Thank you very much, and sorry for bothering