Repeat a Macro Until a Folder Is Empty

Hi there,

Had a look around the forum but can't see any obvious solution. I have created a macro to repeat another macro until a folder does not contain anything. I have pasted the screen shot of my workflow below

Macro's run fine, but once the working path is empty it just keeps on going!

I trigger the test macro (in the above workflow) the macro to execute does it's thing on each folder in the working path and moves the folder to another location, it then triggers the test macro at the end of it's flow and starts again.

Using the attached parameters I would think the test macro would stop when no folders in the working path exist. Unfortunately it just keeps going (disastrous) until I cancel the macro.

Anyway, loving the app, if anybody has an insight I would be very grateful.


I could be wrong, but I think the "nothing exists" condition means the folder itself doesn't exist.

I'm not sure how to see if the folder is empty, though. The only way I can think of is to do a "For each" action to see if there's a file in the folder or not. Something like this:

Perhaps someone else knows of another way.

1 Like

Hi Dan,

Thanks very much for your swift response.

In your explanation above I have completely misunderstood the “nothing exits” term. have tried different variations of if, while, until, etc no luck, this explains why!

Not touched on variables yet so this will be a good intro.

Will revert back with my results,


Since you haven't worked with variables before, let me offer a different version:

In the first version I posted, I used "Set variable to calculation". In this new version, I use "Set variable to text". The reason I'm suggesting this second version is because you haven't worked much with variables, and using them as text is usually easier to understand.

1 Like

Hey Dan,

Brilliant, that did the trick.

Just added my working macro and cancel all macros to the bottom section respectively and worked first time.

Thanks again,


I suspect there’s an easier way to do this, and I hope someone comes along and tells us what it is. Regardless, I’m glad I could help.

Don’t forget to mark the “Solved” checkbox on the post that solved your question. :slight_smile:

Hey Folks,

Well, I’d really want to know what @tubbs is doing in is sub-macro, but there are more than a few ways to check whether a directory has files in it.

From the shell:

du </path/to/your/directory>

When it hits zero the directory is empty.

Here’s one more way:

dirList=$('ls' ~/Downloads/temp/)

if [ "$dirList" = "" ]; then
   echo "My Directory is empty!"
   echo "My Directory is NOT empty!"

And then of course there are ways to use AppleScript.


Thanks Dan! This is very useful for me.

1 Like

Sorry to resurrect this old thread but I can't seem to get it to work in my instance.

I want to loop a set of actions (the highlighted ones) that move image files from one source folder into multiple destination folders. The actions use variables to get unique names from a filename and generate matching folders. Long story. Anyway, that all works and at the end it brings up a KM alert because the regex (which looks for certain named files) isn't found.

So I thought to try and avoid that alert coming up, I could loop it until the folder is empty.

Here is a screenshot of my macro with an attempt to put it all into the correct order DanThomas suggested.

Hopefully some kind soul can help. Many thanks.

You know you can turn off the notification that a regex not found?

Click the gear menu on the upper right corner of the search action, or right-click the action itself to reveal the options menu:


Search using Regular Expression action

If that doesn't solve your problem, we'll move on to the more complex solution.

AHA, thank you, Chris.

I turned off Notify on Failure for my "Search Variable xx Using Regular Expression" part of the macro and low and behold, no more notification. That feels better.

Thanks again.

1 Like