What Happens If... Adding items to folder undergoing "For each...." loop?

Hi All, thanks in advance. I have an awesome KM macro on my laptop, that renames items and puts them in a Dropbox folder. On a different computer, I have a different awesome KM macro (essentially, “For each file in the folder, do a bunch of stuff to the file then move the file to a different folder”) that does some processing on the items in that same Dropbox folder once a day at a pre-set time. This second macro takes about 10 minutes usually.

Normally I don’t do anything to cause new items to be put into that Dropbox folder when that second macro is running. I figure, the Dropbox folder is busy, leave it alone.

But, today I forgot.

There are too many files involved for me to count if everything happened normally.

Question: if a “For… Each” macro is running on dozens of files in a folder; and then new files get put into the same folder as it’s running; what happens?

Thanks!

My guess is that the macro will only process the collection/selection of files that exist in the folder at the start of the macro. The "For ... Each" should operate on that collection.

But I haven't seen your macro, nor have I tested it. :grinning:

That was my guess too. That’s fine, if so… Thanks.

The code in question goes directly to the OS and uses NSFileManager enumeratorAtURL. I have not found any documentation to clarify the consequences of making a change to a directory while you are enumerating it, so the answers is I have no idea what will happen in that case.

To reduce the changes, the best thing to do would be to minimize the operation time, and since you probably can’t do that, the best thing to do would be to generate a list of files, and then process them.

Pseudo-code macro would look something like:

Set variable List to ""
For Each variable Path in directory contents
    Set variable List to "%Variable%List%%Variable%Path%%Return%"
For Each variable Path in lines in variable List
    Do stuff

For good measure, add a Semaphore Lock at the front in case you execute the macro twice.