How to Determine if a Folder Is Empty or Not?

Hi all. I'd like to use an 'If Then Else' action to determine if a folder is empty or not before proceeding with a macro. What would be the best way of achieving this?

I've tried several different options with the 'At this path:' condition trying various combinations but I can't seem to get it to work. When I add files to this folder the state doesn't change from currently false to currently true so I'm a bit confused. Any help or advice would be much appreciated.

Hi. I believe that command is looking for a specific file. Try the ‘nothing exists’ option.

This one works:

(It detects files and folders, but ignores the .DS_Store)

Something there.kmmacros (3.9 KB)

1 Like

The "something exists" test is testing that a file or folder exists at the specified path (ie /Users/wseries/Downloads). If that was ~/Downloads, then something should exist, namely the Downloads folder. The fact that it doesn't leads me to wonder if your home directory is actually /Users/wseries…?

In any event, the condition will not tell you if the folder is empty or not. You can count the files in a folder like this:

Keyboard Maestro Actions.kmactions (1.3 KB)

Note that Keyboard Maestro is not that fast and this will take a few seconds for a thousand files - if you expect a large directory, then a script solution may be better:

ls /Users/wseries/Downloads | wc -l

will return the count of files or folders not starting with a dot in the directory.

1 Like

I am trying to use this to test file count of a folder, but I get this alert message. What is wrong?

You have "the items in directory selected instead of files.

Thanks. I downloaded Peter's actions directly and that's how it appears in KM 9.2. I can't seem to find "files in directory" as an option.

Ah, I see what you mean. Five years makes a difference, I suppose.

But this gglick macro does work.

I tried the same thing using a literal directory named (dragged from the Desktop) and got the same warning you did (which doesn't really matter in this case) and the same failure to count (which does). But using gglick's variable for a selected folder did work.

Update: I had my Display Text action inside the loop that counted. When I moved it outside to report even if there was nothing to count, it did indeed count. Sorry for the confusion.

1 Like

There is nothing wrong with the action.

The warning is letting you know that the index variable Path is never used in the For Each, which is usually an error. In this case, you really don't care about the path of the items, you just want to know how many times the loop runs, so it is fine.

You can remove the warning by referencing Path - even just in a comment. Like this:


Since the warning is just advisory, the check is deliberately very weak.

1 Like

This came in very handy for a macro I am working on today, a (very) belated thank you for sharing.


This is indeed a great alternative to a bunch of actions.
Will save this as a Favorite action