Can Exiftool overwrite existing files?

Started by Andreas Spindler, February 24, 2013, 10:52:08 AM

Previous topic - Next topic

Andreas Spindler

Hi guys.

I'm trying to merge two folders with almost identical images. Only a few images are missing, and in the source folder few images have more recent metadata. I'd like to have ExifTool copy the source folder into the target folder and just overwrite any existing images. With the FMT %f.%e, however, exiftool complains "file already exists" many times. So only the first part of my merge operation works, as the non-existing files are copied.

I know that ExifTool wants to play safe. On the other hand there are too many images involved to use a %c copy number and then manually (or via a shell script) test which are duplicates. The other option would be not to use ExifTool at all, and just cp -u them. Of course cp -u does not access metadata, and I'd like to use -d FMT.

So, I spent two hours reading the manuals and found no solution. Is there a way to convince ExifTool to enter shaky ground and just overwrite the existing files?

Phil Harvey

Sorry, you won't be able to convince ExifTool to overwrite another file.  If I understood correctly, you don't want to use cp because you want to rename the images when copying them?  Why not rename the files first with ExifTool, then cp them?

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Andreas Spindler

Thanks for the quick answer.

Yes, I can use ExifTool to rename and then cp. Now I will use ExifTool to flatten both directories, then cp, then apply -d FMT again.

My question was also general: -d FMT is a powerful option. But when you copy files into an existing directory tree you may end up with zero or more (maybe many) duplicate files buried in that tree. They're literally buried. It's not easy to get these back, especially if some of the target files already had a copy number. You can use fdupes to find identical files, and blindly keep the ones with earlier modification times (unless you used exiftool -P). You can also parse ExifTool's output and cp files it has rejected. But parsing error messages to get uncopied path names just to cp them manually confuses the issue a bit...

Is it a basic principle that ExifTool does not overwrite destination files? Has -overwrite_original any meaning when the target path is different from the source? Because if it didn't I would expect this option to overwrite existing target files as well.

Phil Harvey

#3
Yes, it is a basic philosophy that ExifTool won't overwrite files.  However, I did make an exception with text files by adding the "!" feature to the -w option.  What you are proposing would be implemented with a similar feature on the -o option, but I don't want to do that because it is just too easy to lose images that way.

The -overwrite_original option causes exiftool to delete the source file (ie. move the image) instead of copying the image when the target is different from the source.  But it has no effect when just moving the file by writing FileName and/or Directory only (since no backup copy is generated in this case).

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Andreas Spindler

I understand. So when just copying/moving the file (-d FMT plus writing the file tags) some files might be copied/moved, others not. This actually puts the target directory in an "undefined state". When copying hundredths of files it is hard to keep the overview. I have this problem occasionally. Mostly the error message "file already exists" scroll so fast you don't even really notice that some files are not copied.

In databases one would use a transaction for that - in order to make sure that either everything is copied/moved, or nothing.

Phil Harvey

If you put a %c in the destination name, then you know all of the files get copied.

If you don't put a %c then you know that duplicates won't get copied, and then it is up to you to figure out how to handle these.  Unfortunately though, this seems to be the problem you are struggling with.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Andreas Spindler

Quote from: Phil Harvey on February 24, 2013, 04:01:45 PM
If you put a %c in the destination name, then you know all of the files get copied.

If you don't put a %c then you know that duplicates won't get copied, and then it is up to you to figure out how to handle these.  Unfortunately though, this seems to be the problem you are struggling with.

- Phil

Actually, merging two image directories with an overlapping set of files is difficult anyway.

But this was my starting point. %c is good to create unique filenames, but duplicates have to be tracked manually after ExifTool is done. The solution was not to use %c, and parse ExifTool's output for files that already existed, and then cp them.

Thank you for your help. I did not know that ExifTool will never overwrite files when the input/output paths differ.