How Do I Add only new folder names to a file?

Keyboard Maestro 8.0.3 “Add only new folder names to a file


I have a text file containing a directory tree. In order to automatically update the list of folders when a new folder is created on the disc I would like KM to read the text file, go through a folder recursively and check if the specific folder already exists in the text file and add it to the end of the file if the folder does not exist.

Unfortunately I am struggling with making the test if the folder already exists and react accordingly, and would appreciate some help to make it work. Any ideas what goes wrong?

ArchiveRulesAutoUpdate.kmmacros (3.9 KB)

As near as I can tell, searching and replacing a variable with identical values will simply end up copying that variable to wherever it is replaced (the found variable in the case of your original macro) so found will never end up starting with "nothing found" even when it's supposed to. I suspect that may have been the primary reason why your macro didn't work the way you expected.

Here's another way I figured out that seems to fit your requirements. Every time a new folder (or file, so be wary of that) is added to the folder (Desktop, in this case) it checks the text file specified in the beginning for the number of times the path of the newly added folder/file matches a line. If the number is not 0 (i.e. one or more matches has been found and the folder/file already exists) it leaves the file alone; if it is 0, then it appends the new folder/file's path to the text file.

ArchiveRulesAutoUpdate 1.1.kmmacros (4.2 KB)

Hi gglick

it works like a charm on the top level of the folder - thank you for the tip!

Unfortunately my folder structure is 4-5 levels deep, so nothing triggers in my normal use case where I create folders deeper in the structure.


Ah, I see what you mean now. Sorry for misunderstanding. In that case, I believe this macro should do the trick:

ArchiveRulesAutoUpdate 1.2.kmmacros (3.1 KB)

Unfortunately this version can't make use of the TriggerValue token to automatically add entries to the text file, but it should search every folder (and file) recursively in the folder you specify, including its subfolders, and add the path of any newly added folder or file however you decide to trigger it. Give it a try and let us know if it still doesn't do what you want.

Awesome - thank you so much for your help! I will figure out how to filter out files so that it only adds newly added folders.

And your initial proposal gave me an idea of how to easily use KM instead of Hazel with a python script for auto archiving into the folder structure.

1 Like