1
\$\begingroup\$

I use the following command to bulk rename images from inside a bash script so each image is named according to the date the picture was taken:

exiftool -r -f '-filename<${CreateDate}_${model;tr/ /_/}_%f.jpg' -d %Y%m%d_T%H%M%S%%-c -ext jpg $targetDir

Sometimes, more often than I'd like, I encounter images that have incorrect values for dates in the metadata. For example:

$ exiftool -*date -G -s  *.jpg
[File]          FileModifyDate                  : 2014:12:22 18:32:03-07:00
[File]          FileAccessDate                  : 2014:12:23 11:41:23-07:00
[File]          FileInodeChangeDate             : 2014:12:23 11:40:00-07:00
[EXIF]          CreateDate                      : 2014/12/23 00:52:41

In this example, the FileModifyDate is the date I would have expected to be in the CreateDate tag, that is to say that the picture was taken 2014:12:22 18:32:03-07:00.

But because the CreateDate is not correct, the new file name is not correct. Well, from the perspective of the command used to rename the file it is correct (garbage in -> garbage out), but I would like the date used in the file name to be the date/time when the picture was actually taken.

In this example, is there some way to query the file for all date tags, and then select the earliest date available as the date to use in the renamed file name?

Also, using '${model;tr/ /_/}' I can replace any spaces that might be in the model tag with underscores. This is nice, but I would rather wrap this expression around the entire new file name. That way, since I'm using '%f.jpg', if the original file name contained any spaces, the resulting file name would not contain any spaces. Is this possible with exiftool?

\$\endgroup\$
4
  • \$\begingroup\$ I suspect you're more likely to get help here: superuser.com/questions/tagged/exiftool \$\endgroup\$
    – MikeW
    Commented Dec 23, 2014 at 21:28
  • \$\begingroup\$ I also get the problem that the exif date is off by a constant amount on all files. You might figure out the offset and then derive the correct time for the whole set. After all, a camera won't put a random different time on every shot; it will have the clock set wrong. \$\endgroup\$
    – JDługosz
    Commented Dec 24, 2014 at 1:32
  • \$\begingroup\$ Or maybe the timezone of the computer or camera is not set properly \$\endgroup\$ Commented Dec 24, 2014 at 8:18
  • \$\begingroup\$ How do you move a question from one site to another without duplicating it? \$\endgroup\$
    – axiopisty
    Commented Dec 24, 2014 at 15:41

2 Answers 2

2
\$\begingroup\$

I have added the required steps here:

https://github.com/pratyushtewari/exif-scripts/blob/master/README.md

Set to oldest date from Exif meta or file name

Step 0 - install exiftool from below

Installing ExifTool

👉 Step 0.1

Save the oldest_datetime_config in the home folder C:\Users\YOURUSERNAME or in your home directory ~ or in the same folder where you have installed exiftool. This step is important for the following steps to be successful. Rename the file to .ExifTool_config

You can find these in a script here but I have found that running the commands in the pic folder I mentioned in the options below are much much faster than running these through python script exif-scripts/setToOldestDate.py For faster processing you can run these commands directly in powershell from the pic folder you are planning.

If you are running WSL then run these commands from poweshell and not WSL are the file system read write from the windows virtualization will blow up your ram and will slow down the process to a halt.

🦺 Note that these scripts are recursive and will affect all the subfolders are well.

Well … now — pick your options:

Option 1 - In case you need to pick the date from the file name.

This is pretty flexible and fuzzy parser from exiftool but if you want it to be absolutely sure then you can remane the file to include yyyymmdd_hhmmss and remove all the other number before it (after is OK).

exiftool "-FileModifyDate<filename" −overwrite_original -S -m -progress -ee -q -q ./

Option 2 - set all the dates to the oldest date

exiftool -overwrite_original "-FileModifyDate<OldestDateTime" "-ModifyDate<OldestDateTime" "-DateTimeOriginal<OldestDateTime" "-CreateDate<OldestDateTime" "-GPSDateTime<OldestDateTime" -S -m -progress -ee -q -q ./

Option 3 - set the name of the file in PXL_yyyymmdd_hhmmss-[number-if-conflict].extension

exiftool "-filename<OldestDateTime" -d PXL_%Y%m%d_%H%M%S%%-c.%%e -S -m -ee -progress -q -q ./

to push all the files in the current directory use

adb push -a . /sdcard/DCIM/Camera/

-a is to preserve the attributes.

⌛ Refresh Android Media Store**

As a last step, you need to refresh the android media store database to let google photos know which new files to backup. Currently it does not always detect automatically so why wait for that. Install Refresh MediaStore and run it after the adb push is complete from the phone. Then you can open Google Photos and see that the backup is starting.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you! One important step I overlooked is to name exif script as .ExifTool_config \$\endgroup\$ Commented Aug 4, 2022 at 1:38
  • 1
    \$\begingroup\$ Oh yes. I did not keep the name with a dot because the moment you download it, it was hidden and people kept looking for it. lol. I will add that in the readme itself. \$\endgroup\$
    – Pratyush
    Commented Aug 5, 2022 at 3:16
1
\$\begingroup\$

You should be able to do some more crazy perl expressions inside those {}s. (I'd be more tempted to write something this complex in another language, actually, but for the sake of argument):

${CreateDate; use Date::Parse; @vals = strptime($_); sprintf("%-04d_%-02d_...", $vals[5], $vals[4]);}

Note: completely untested code. I doubt it works. But it gives you a direction to start, if you know perl.

\$\endgroup\$
1
  • \$\begingroup\$ +1, because, yeah, something like that. :) And also, a pointer to what may be a better approach: gexiv2 python bindings \$\endgroup\$
    – mattdm
    Commented Dec 24, 2014 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.