Avoid errors when copying files that already exist in the destination

Started by Joanna Carter, August 25, 2018, 04:50:54 AM

Previous topic - Next topic

Joanna Carter

Hi there

I am "sorting" a source directory hierarchy into a destination date-ordered hierarchy but, as can happen, the source hierarchy contains duplicate files. Consequently, when these files are copied, I get an (expected) error to the console, but the command continues anyway.

Error: './f:32.jpg' already exists - /Volumes/Joanna C/DCIM/Cours/Exposition/f:32.jpg

Is there any way to inhibit this console message?

Phil Harvey

Error messages can not be silenced.  You need to redirect the STDOUT and STDERR in your app to capture these.

Take a look at my C++ wrapper for an example of how this is done in C++.

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

Joanna Carter

Aaarrgghhhhh!!!!

I haven't touched C++ in anger for over 25 years  ;D

Nevertheless, I did find the Swift equivalent, thanks to your code.

The only problem I have now is trying to get STDOUT and STDERR to respond after I've finished intercepting them.

Although it is quite nice to have an empty console display  ::)

Joanna Carter

Rather than suppress the error messages, is there a switch that will automatically add a version number to the file name?

I tried /Users/joannacarter/Pictures/%Y/%m/%d/%%f%%c in exiftool -r -ext JPG -o . '-Directory<DateTimeOriginal' -d /Users/joannacarter/Pictures/%Y/%m/%d /Volumes/Joanna\ C/DCIM/ but that really messed things up  :o

??? ???

Phil Harvey

The %c is what you want.  You add it to the format string when setting the FileName.

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

StarGeek

To be clear, you can't use it with -Directory to work properly, you have to use -Filename and set the full filename, including the extension .%e or %E
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Joanna Carter

Hmmm. I still seem to be missing something. I am still getting errors like:

Error: './JNA_0001.jpg' already exists - /Volumes/Joanna C/DCIM/Projections/2018/juin/JNA_0001.jpg

Here is the command line I am creating:

exiftool -preserve -extension jpg -recurse -ignoreMinorErrors -q -q -extension jpg -out . -FileName<DateTimeOriginal -d /Users/joannacarter/Pictures/%Y/%m/%d/%%f%%-c.%%e /Volumes/Joanna C/DCIM/
And there is only ever one file with the original name

Might this have something to do with this from the -w docs?
QuoteThis same FMT syntax is used with the -o and -TagsFromFile options, although %c is only valid for output file names

Phil Harvey

You need to add quotes around "-FileName<DateTimeOriginal".

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

Joanna Carter

???

Unless I start playing around with %c, everything else works fine, even without the quotes. Don't forget this is not being typed into the terminal.

exiftool -preserve -extension jpg -recurse -ignoreMinorErrors -q -q -extension jpg -out . -FileName<DateTimeOriginal -d /Users/joannacarter/Pictures/%Y/%m/%d/%%f.%%le /Volumes/Joanna C/DCIM/ works fine, apart from kicking up:

Error: './f:32.jpg' already exists - /Volumes/Joanna C/DCIM/Cours/Exposition/f:32.jpg

However, if I change the command in the hope of avoiding the "already exists" errors by appending a "copy number":

exiftool -preserve -extension jpg -recurse -ignoreMinorErrors -q -q -extension jpg -out . -FileName<DateTimeOriginal -d /Users/joannacarter/Pictures/%Y/%m/%d/%%f%%-c.%%le /Volumes/Joanna C/DCIM/

... nothing changes. The file names are all exactly the same as without the %%-c and the errors are still produced.

Phil Harvey

Try it from the command line.  I did and it worked as expected.  The error is somehow in your running of the command from Swift.

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

Joanna Carter

Sorry Phil but I'm not getting the same results here.

Copying the command from my code to Terminal, surrounding -FileName<DateTimeOriginal with single quotes and escaping the space in the source directory gives me:

exiftool -preserve -extension jpg -recurse -ignoreMinorErrors -q -q -extension jpg -out . '-FileName<DateTimeOriginal' -d /Users/joannacarter/Pictures/%Y/%m/%d/%%f%%-c.%%le /Volumes/Joanna\ C/DCIM/

Running that gives me exactly the same "already exists" errors and lack of copy numbers as running it from my app  :-[

I must clarify that, apart from that, the command does exactly what I want it to do, whether from the Terminal or from my app.

When you are trying it, does your source hierarchy contain duplicate files in different sub-directories?

Phil Harvey

> exiftool a.jpg -datetimeoriginal                                                       
Date/Time Original              : 2018:08:27 07:31:40
> exiftool -preserve -extension jpg -recurse -ignoreMinorErrors -q -q -extension jpg -out . '-FileName<DateTimeOriginal' -d tmp/Pictures/%Y/%m/%d/%%f%%-c.%%le -v a.jpg
======== a.jpg
Setting new values from a.jpg
'a.jpg' --> 'tmp/Pictures/2018/08/27/a.jpg'
Created directory tmp/Pictures
Created directory tmp/Pictures/2018
Created directory tmp/Pictures/2018/08
Created directory tmp/Pictures/2018/08/27
Rewriting a.jpg...
Nothing changed in a.jpg
> exiftool -preserve -extension jpg -recurse -ignoreMinorErrors -q -q -extension jpg -out . '-FileName<DateTimeOriginal' -d tmp/Pictures/%Y/%m/%d/%%f%%-c.%%le -v a.jpg
======== a.jpg
Setting new values from a.jpg
'a.jpg' --> 'tmp/Pictures/2018/08/27/a-1.jpg'
Rewriting a.jpg...
Nothing changed in a.jpg


Note that the second time I ran the command it added "-1" to the file name.

Can you replicate this?
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Joanna Carter

If I cd to the source directory, and try your first command

/Users/joannacarter/Développement/PhotoBrowser/PhotoBrowser/Resources/exif/exiftool a.jpg -datetimeoriginal

I get absolutely nothing, not even an error.

If I then copy your "full" command and simply change the destination directory:

/Users/joannacarter/Développement/PhotoBrowser/PhotoBrowser/Resources/exif/exiftool -preserve -extension jpg -recurse -ignoreMinorErrors -q -q -extension jpg -out . '-FileName<DateTimeOriginal' -d /Users/joannacarter/Pictures/destination/%Y/%m/%d/%%f%%-c.%%le -v a.jpg

... I get the following output:


======== a.jpg
Setting new values from a.jpg
Error: './a.jpg' already exists - a.jpg


Directory structure is:

/Users/
-joannacarter/
--Pictures/
---source/ (cd to here before running command
---destination/

Anything to do with not having ExifTool installed in its default location, only in my app's project?

Phil Harvey

Quote from: Joanna Carter on August 27, 2018, 11:46:09 AM
If I cd to the source directory, and try your first command
I get absolutely nothing, not even an error.

OK.  That's the problem.  The %c only applies to the DateTimeOriginal value.  If there is no DateTimeOriginal, then  '-FileName<DateTimeOriginal' does nothing and the %c in the date/time string isn't used.  What did you intend to happen to files without a DateTimeOriginal?

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

Joanna Carter