OK, so if I understand correctly, you want to iterate through the files in ~/Desktop/Export, and then you see, for example d1.xlsm (are they always .xslm files?). So you look to see where d1.xlsm is in ~/Desktop/Import, and it will appear in exactly one location, in this case ~/Desktop/Import/STL-4/d1.xlsm, so you want to move/rename the file to ~/Desktop/Export/STL-4/d1.xlsm (creating the subdirectory STL-4 as needed).
And there are no files directly within ~/Desktop/Import (only directories)?
And no directories initially in ~/Desktop/Export (only files)?
And there is only one level of directories in ~/Desktop/Import?
With that clarity it should be fairly straight forward to write the macro/script.
Before starting, ensure you have your Mac fully backed up. Any time you are going to automate moving files around or anything like that, you need to contemplate the consequences of your macro being incorrect and you moving all the wrong files to all the wrong places. So complete backups are absolutely essential.
So to accomplish this task (or any task really) you break it in to component parts.
Iterate through the contents of the folder. Use the For Each action with the Folder Contents collection.
For each of those, you may want to use an If Then Else action with a Variable condition to ensure the path ends is .xlsm or .xlsx or .docx depending on your desires so you only process files you expect to find.
Use the Split Path action to extract just the filename.
Now you have the trickiest part, how to find the location of the matching file in the Import directory. Probably the easiest way to do that is to use the unix find shell command.
find ~/Desktop/Import -name "$KMVar_FileName"
That will return the path to the matching file - save it in a variable. Now you have the original path.
Use the Split Path action to get the parent path (you know the name already), and then again to get the name of the directory.
Now create the directory with the New Folder action (and configure it to ignore and not report errors in creating the directory if it has already been created): New Folder ~/Export/%Variable%ParentFolder%.
And finally you can move the file with the More or Rename File action. Move %Variable%Path% to ~/Export/%Variable%ParentFolder%.
And that should pretty much do the trick.
You can implement each part as you go, using the Log action to record what is happening or what you'll do next in the Engine.log file.
I don't see anything obviously wrong with your Actions.
What exactly is the result when you run the Macro?
Of course, make sure there are actually some file in the folder.
As an alternate, you could use this to list all files in the folder: find ~/Desktop/Export -maxdepth 1 -type f
I guess that when doing everything right, I'd have to removed the matched line from the variable AllImportFileNames every time? This would speed things up when you have many folders and files.
Never bother trying to improve the speed of a macro until you are annoyed by the speed of a macro. Otherwise, use the simplest macro you can possibly make and let the fantastically fast CPU do its work.
If you have large numbers of files in the import directory, then yes, this might start to get slow. There are solutions to that, but removing the matching lines is probably not one of them - that would only half the time. Instead, you would pre-populate a dictionary and use that instead and that would be much faster in the case where there are large numbers of entries. But definitely don't bother until you actually need to - it probably will be fast enough as is.
I want to make the macro more flexible by allowing to run it on the selected folder on the Desktop (with a name that can contain spaces and umlauts). I'm having difficulties to have the Path passed correctly to the shell script:
You've mis-cased part of the KMVAR prefix for shell variables in the find command.
Easy to do.
Hard to see.
When shell scripts fail in Keyboard Maestro you should always make sure they work properly outside of Keyboard Maestro (when possible).
Especially when you're passing variables around and messing with quoted (or escaped) path strings.
The place to start is to pull the fully assembled shell script out of Keyboard Maestro and into a good programming/text editor like BBEdit – examine the syntax – and see if it works.
Here's how I did that with your code:
I turned off everything inside the For Each action except this:
** Note that I changed your variable name to pathStr. (I vehemently dislike using Path as a variable name.)
Other than the variable name change the find script is verbatim from your macro.
Using cat with a here document in this way lets me expand all the variables and other elements of the shell script without executing it – and I don't have to change any of the quoting to get it to work.
The result was:
find "" -iname "*.xls"
That's pretty clearly a variable problem, so I looked a third time a $KMVar_pathStr and finally realized a couple of letters were the wrong case.
Once I fixed the case issue your macro worked fine.
And it took me longer to write this up than it did to debug the problem.