Renaming a File if a Another File With the Same Name Exists

I have this:

image

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

There was a similar discussion about folders recently.

1 Like

Keyboard Maestro 10 has a new additional feature "Create Unique" which creates a new incremental file and name if a file already exists. This can be used to do what you want.

"Create Unique" doesn't work the way I imagined it would but it still achieves the task with a minimum of Keyboard Maestro Actions.

For testing, the below Macro is set to "copy" rather than to "move". To adapt it to move selected files change "copy" to "move" in the two green Actions.

EXAMPLE Copy File to Folder and Increment if Name Already Exists.kmmacros (18.8 KB)

Click to Show Image of Macro

EXAMPLE Copy File to Folder and Increment if Name Already Exists

And for an explanation of how the above works:

"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...

4 Likes

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.).


Move File and Rename if Name Exists.kmmacros (13.5 KB)


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:

  1. It moves the file to the destination folder, if there is no naming conflict.
  2. 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.

Example:

Before (where "Folder A" is source and "B" is destination):

Screen Shot 2022-12-06 at 16.56.34-pty-fs8

After:

Screen Shot 2022-12-06 at 16.56.51-pty-fs8


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.

– Tom

2 Likes

Here's a neat variant -- product of me looking at the %CalculateFormat% token last night. You can use that to format the appended "counter":

%CalculateFormat%Local__i%-000% --> MyFile-001
%CalculateFormat%Local__i% (0)% --> MyFile (1)
%CalculateFormat%Local__i% (Copy 00)% --> MyFile (Copy 01)

...and so on.

Move File with Rename if Exists.kmmacros (5.1 KB)

Image

You could change it to use @Tom's final "Try/Catch" method rather than my "test until safe name found", then you'd only have the nastily-long destination calculation in there once.

3 Likes