Help with Renaming Files using For Each Loop

Hi, I'm trying to get this macro to update all files added to a folder, but it's only updating the first one.
The files are named similar to:
IMMUNE INTENSIVE Transcript Lesson 1 Class 1.pdf

Rename Transcript Files.kmmacros (3.8 KB)

First, you're asking for a bit of trouble by having a trigger of “file added to folder” and then making changes to that folder, which may well re-trigger the macro a second time while it is still processing.

Since your changes are presumably idempotent, re-running the macro should be fine, but you do not want the two instances running at the same time, so I would put a Semaphore Lock action at the start of the macro. The macro will likely then run a bunch of times but later ones will not make any changes so that should be ok.

I'd guess your issue is that the Search using Regular Expression action is failing and aborting when it hits a file that it can't change (or already has changed). That should notify you of the failure, but perhaps you have not got notifications for Keyboard Maestro Engine enabled - the error will be reported in the Engine.log file, which you can check (Help ➤ Open Logs Folder). Using Keyboard Maestro without enabling notifications for the engine is generally a bad idea since you wont see such errors and will be left guessing why the macro stopped.

You will need to adjust your macro to allow for files that have already had their name changed and ignore them and generally ensure that files once changed are not changed further since the macro will run potentially many times.

yes, this is an inherent problem with folder trigger + renaming. ie, renaming a file creates a trigger loop.
My solution was to break the function into 2 phases - first dump the file to a secondary folder - which does not have a trigger - and rename it there. I used AppleScript to move the file & my script kept track of the new location, to pass back to KM

1 Like

Thanks

Thank you