If I have a file in that destination with the same name, it shows me an error and it doesn't move the file. Is there a way to check if there's a file with the same name in the destination folder and rename the file I'm about to move, but adding a number at the end by incrementing it? So if the file is file.jpg, the new one is file1.jpg. If it's file1.jpg, it becomes file2.jpg, etc
"Create Unique" looks in the destination folder and if a file with the name already exists it makes a new blank file in the destination folder with a unique incremental name. It also copies that new file's complete path to a Variable. We only want this Variable - we do not want to keep the file that Keyboard Maestro has just made (as this file is an empty file rather than a copy of the source file).
So, we delete this file but use the very useful Variable that "Create Unique" has made, to move/copy the file in the source folder to the destination folder...
Here an approach without the Create Unique File action. This has the advantage that you can freely choose your unique naming scheme (e.g. <name> 001.<ext>, <name>---01.<ext>, <name> (01).<ext> or <name>1.<ext>, etc.).
Please treat this macro as experimental! I tested it only briefly.
To run the macro you have to set your source and destination folders in the (green) "Setup" group to something that exists on your volume.
Set a trigger in the macro header or run it from within KM.
Moving/Renaming files can be destructive! So, try it first with two test folders, filled with test files. Double-check that the paths in the first two actions correspond to your test folders!
The macro should do this:
It moves the file to the destination folder, if there is no naming conflict.
If there is a conflict, then the Repeat loop (orange) comes in:
With the given setup, it appends <space>001 to the filename and tries again.
If this fails too, it tries with <space>002, and so on.
The Repeat loop ends if…
the file was successfully moved, or…
the max number of repeats (last action in the green group) is reached. In this case the file will not be moved or copied, it just remains in the source folder.
It should also work with files that do not have an extension.
The magenta-colored actions are for debugging. You can safely remove them, if you think the macro works correctly.
Before (where "Folder A" is source and "B" is destination):
Instead of a repeat loop, you could probably implement the Try/Catch block as subroutine and use it as a truly recursive function. This would perhaps be a bit more elegant, but would result in 2 separate macros, so I decided for the Repeat solution.