1

Using exiftool to write in filename either CreateDate or FileModifyDate, whatever exists.

What is the problem: -If no CreateDate exists, error is happening, and filename is not changed according to Creation date.

"Warning: No writable tags set from DSC_0680a.JPG"

How can I tell exiftool to write either CreateDate or FileModifyDate - whatever exists in exif information?

Currently, I am using the following command:

for pic in DSC*.*; do exiftool "-FileName<CreateDate" -d ${pic//.*}_%Y%m%d_%H%M%S.jpg" "$pic"; done;

This does not work too:

exiftool "-FileName<CreateDate" -d "DSC_0680a_%Y%m%d_%H%M%S.jpg" DSC_0680a.JPG  || exiftool "-FileName<FileModifyDate" -d "DSC_0680a_%Y%m%d_%H%M%S.jpg" DSC_0680a.JPG 
Estatistics
  • 874
  • 9
  • 24

2 Answers2

2

Using exiftool in a loop can greatly extend processing time as the startup is its biggest performance hit (see ExifTool Common Mistake #3).

You don't need two separate exiftool commands. You can put both options in the same command and exiftool will use the last valid option (see Note #1 under the -TAG[+-^]=[VALUE] option).

You could use

exiftool "-FileName<FileModifyDate" "-FileName<CreateDate" -d "%%f_%Y%m%d_%H%M%S.%%e" /path/to/files/

and exiftool will fallback on FileModifyDate if CreateDate doesn't exist

Here I did remove the ${pic//.*} and replaced it with exiftool's %f and %e variables, which stand for the Filename and Extension respectively (see the -w (-TextOut) option). When used in a date format string, the percent signs for filename variables need to be doubled.

It should be noted that if used in a Windows Bat file, then all percent signs need to be doubled, which would result in a -d (-dateFormat) string of %%%%f_%%Y%%m%%d_%%H%%M%%S.%%%%e (see ExifTool FAQ #27).

StarGeek
  • 4,948
  • 2
  • 19
  • 30
0

I tried this command with the linux operator "&&", "which means whatever happen to the first command, execute the second one too", and it worked (?).

  • The filename that it is changed by the first command,
  • it is not changed again from the 2nd command, because,
  • it keeps the filename that it was passed from the first command,
  • and no more it exists, ecause it changed.

My exiftool command to change / rename filename according to CreateDate OR FileModifyDate that exists in image information tags.

  for pic in DSC*.*;        do exiftool "-FileName<CreateDate" -d  "${pic//.*}_%Y%m%d_%H%M%S.jpg" "$pic"   &&   exiftool "-FileName<FileModifyDate" -d  "${pic//.*}_%Y%m%d_%H%M%S.jpg" "$pic"   ; done;
Estatistics
  • 874
  • 9
  • 24
  • 2
    You don't need two separate exiftool commands. You can put both options in the same command and exiftool will use the last valid option. So you could use `exiftool "-FileName – StarGeek Jul 11 '20 at 20:40
  • Additionally, using exiftool in a loop can greatly extend processing time as the startup is the biggest performance hit (see [Common Mistake #3](https://exiftool.org/mistakes.html#M3)). I'm not familiar with your script (linux or mac?) but I'm guessing that `${pic//.*}` is the filename without the extension? If so, you can drop scripting altogether with a single exiftool command `exiftool '-FileName<%f_$FileModifyDate.%e' '-FileName<%f_$CreateDate.%e' -d '%Y%m%d_%H%M%S' /path/to/files`. This will use exiftool's filename tokens for the filename and extension. – StarGeek Jul 11 '20 at 20:45
  • @StarGeek : Consider putting your comments in an actual answer, so they are more visible. They are indeed the solution. – mivk May 27 '22 at 10:19
  • @Estatistics : `&&` in a Unix shell means "and". It does NOT mean "whatever happens to the first execute the second"! It will only run the second command if the first one was successful, and didn't exit with an error. Try these: `true && echo "success"`, `false && echo "success (not)"`, `false || echo " '||' means 'or'. This was false "`. However `exiftool` does not always exit with an error when you might expect that it would. And in this case, it still exits with "0" (success) even if it did not rename the file. – mivk May 27 '22 at 10:41
  • @mivk blast from the past. I'm trying to remember why I didn't put this as an answer. I faintly recall there was some reason for it, but it escapes me ATM. – StarGeek May 27 '22 at 14:32
  • @StarGeek you may put this as an answer now... :) – Estatistics May 30 '22 at 08:40