[SOLVED] Rename Folder When New File Is Added/Deleted

I have a folder called "Work - 0".

Now every time I add a new file/folder to that folder, I would like it to be renamed and instead of 0, it would show the number of files inside that folder, so if I drop 4 files there, it would then become "Work - 4". If I then delete 2 files, it renames it "Work - 2".

It would only read "sub" files/folder, not sub sub files/folders. So if I have 2 folders inside with 30 files inside each, it would still just rename it "Work - 2", not "Work - 62".

Is this possible?

The trouble here is that whenever you rename the folder (to show the file count), it will mismatch with the folder trigger path. I was hoping you could set the folder trigger to, for example, ~/Desktop/Work -.* in order to match any folder name that starts with Work -, but no joy there. Perhaps some clever sausage will come up with a way to do this...

As a workaround, how about a macro that briefly displays the number of files/folders in the selected folder?

Show Number of Files in Selected Folder.kmmacros (21 KB)

Macro screenshot

1 Like

Only thing I could think of was to have the folder you add to/remove from and also an alias to that folder that you used for display purposes, updating the alias's name as the content count changed.

2 Likes

One (clunky) approach would be to use a global variable for the folder name. So when you set WorkFolderName to Work - 3 to change the name, it's retained. The next time the macro detects a content change in the folder, it references WorkFolderName instead of looking at the actual folder name and uses that to update the name with a temporary variable copied back to WorkFolderName after success.

Do you mean like this?

If so, it won't work as you can't use a variable as a folder trigger.

Yeah that's what I thought...

The goal is to actually have the folder keep the number, especially because I'm using a single folder as an example, but I want it actually work for several folders. And the goal is to being able to add files, work on something else, and when I come back, I know exactly where I stopped.

There are 2 things as well:

  1. The macro needs to be triggered when the folder changes, which seems that your macro is not doing that?

  2. I won't be selecting the folder (or folders), so your action with "%FinderSelection% wouldn't work, right?

This could be a workaround. I could have a main folder with the real folders (because I will be using more than 1, even though I used 1 as an example) and then have another location with the Aliases and those would update the name.

But how would KM know that the file to update is the alias, since the name will always be different?

Ah, you need the folder name for the trigger. Missed that.

I think I'd go in a different direction from renaming the folder (which would force a backup every time Time Machine ran). A notification maybe.

I don't really use Time Machine, but forcing a backup isn't a problem anyway.
The issue seems to be the name being different every time.

Can't KM scan all folders inside a main folder and check if they have a specific name and if so, rename it? For example:

if there's an item with "work - ", rename it "work - number of items"

I know this sounds similar to what you said, @noisneil, but not quite. In this case we are triggering the main folder, so for example, every time the main (parent) folder's size changes (or the modified date changes), it triggers the macro. The macro then scans the content inside that parent folder and renames the folders. Does it make sense?

Yes, that's a problem. And that makes me wary of using a folder name for a file count. I think I'd rather just have a popup window that persists in the corner of the desktop if a notification isn't sufficient.

I like the idea! So I suppose we'd have to use a global variable to keep track of the alias name. With that in mind:

Update Folder Alias Name with File Count.kmmacros (25 KB)

Macro screenshot

Setup:

  • Move your "Work" folder somewhere for safe keeping. Make an alias of it and move the alias to wherever the original was before. Don't rename the alias.
  • Set the macro's folder trigger to the original folder in its new location.
  • Enable the red group, drag a file to the alias, and then undo. This is just to set a starting value for the OldAliasPath global variable.
  • Disable the red group.

Now, try adding or removing files from the alias (or original) folder and you should see the file count reflected in the alias name, like so:

CleanShot 2022-11-01 at 19.13.20

NB: If you want to use this for multiple folders, you'll need a unique global variable for each one. OldAliasPath1, OldAliasPath2, OldAliasPath3, etc.

3 Likes

I saw that on your macro, there's a yellow triangle on the "FOR EACH" action:

image

When I click I see this:

image

Did you miss something? Is there a typo?

Don't worry about that. The For Each action often shows that alert. I can't remember why now, but Peter did once explain that you can ignore it.

I was thinking a Dictionary would be better. Something like

{
    {
        OriginalFolder: /path/to/original/folder 1
        AliasFolder: /path/to/alias/folder 1-
        Count: 3
    }
    {
        OriginalFolder: /path/to/original/folder 2
        AliasFolder: /path/to/alias/folder 2-
        Count: 17
    }
}

You should then be able to use one macro with multiple watched folder triggers, pulling the OriginalFolder from the %TriggerValue%, composing paths from the relevant Dictionary entry, then updating the relevant Count value to the new item count.

To my shame I still don't know enough about KM Dictionaries to knock up a POC. Maybe tomorrow...

The trouble is you can't get the alias path from the triggervalue. Having said that, I guarantee I know less about dictionaries than you do, so maybe I've missed your point...?

I guarantee I know less about dictionaries than you do!

Having thought about it, it's because we're just counting the number of items, and not actually using the variable to do anything. It isn't really an error, but the Editor will still warn you about it.

1 Like

With the Dictionary as above you can ask for:

the AliasFolder value of the Dictionary item whose OriginalFolder has the value '/path/to/original/folder'

...and:

the Count value of the Dictionary item whose OriginalFolder has the value '/path/to/original/folder'

...then concatenate the two to get the path to the currently-named alias. (And that's why you have to update Count to the new value, after you do the rename, ready for next time.)

You should be able to do this when you drop a file/folder into the OriginalFolder, and I'm assuming that %TriggerValue% will include the path to OriginalFolder even when you drop a file into the alias (it is, after all, OriginalFolder that's changing and therefore firing the trigger).

Yes, it does. :+1:t3:

There isn't a "folder size change" trigger though. You might be able to use the front window trigger, which would do nothing unless you open the parent folder in a Finder window (i.e. the Desktop wouldn't be a great choice). It could then run the count when you go to actually view the folder.