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