Conditional rename if enclosing folder date hierarchy ≠ exif "Date Created" …

Started by clem, January 05, 2021, 11:38:13 PM

Previous topic - Next topic

clem

I'm continuing to reconstruct dates, times etc. for photos that have been stripped of information. One of the interesting things I've noted is that sometimes the information is 'there' but in an unconventional form - folder hierarchy.

A file is found here in a form of a folder hierarchy of YYYY-MM-DD with each level representing the YEAR, then the MONTH, then the DAY:

2013
-----12
-------31
---------2016-06-06 083245.jpg


In a flat folder presentation with ƒ representing a folder level, it would be
2013 ƒ 12 ƒ 31 ƒ 2016-06-06 083245.jpg

The JPEG located in it has instead a totally different date as its filename, in this case 2016-06-06 AND it does NOT have "Date Created" or "Date/Time Original" or "Create Date", so it is named 2016-06-06 083245.jpg

So if a JPEG has no "Date Created" or "Date/Time Original" or "Create Date" tag, I'd like to get exiftool to extract it from the hierarchy of the enclosing folder which will always be in the format of YYYY-MM-DD, i.e. a nested set of folders, each set containing only YYYY or MM or DD as folder value. Then of course, create the three families of tag for Date Created / Date/Time Original and CreateDate.

As for time, a creation time of exactly 00:00:00 would be great since the time is likely the time the file was generated / renamed and not relevant. This would be also a useful way to keep track of files that have been renamed using this technique since it is unlikely that I will be taking hundreds of pictures at exactly midnight on a regular basis. :-)

thanks as always for tips and suggestions!

Phil Harvey

You can do it like this:

1. "cd" to the top-level directory

2. exiftool -if "not $createdate and not $datetimeoriginal" "-createdate<$directory 00:00:00" "-datetimeoriginal<$directory 00:00:00" -ext jpg -r .

This will set CreateDate and DateTimeOriginal from the directory structure if not already set for all JPG images in or within the current directory.  I don't know what you mean by "Date Created", so I didn't do that one.

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

Luuk2005

This very long and extra commands, so the experts can say what is not to be needed, so please not to try, until hearing the advice!
The $XMP:DateCreated is only guessing, so maybe instead you like IPTC:DateCreated with IPTC:TimeCreated?
This very long command to test for: (3 $Tags not found) and folderpath must end like your explanation. There is extra colons for the eyesight, and exiftool puts the date with space in front of filename, so then to remove previous date with space, if there was one, but only at beginning of filename.

exiftool -r -ext jpg -if "not ($XMP:DateCreated or $Exif:DateTimeOriginal or $Exif:CreateDate) and $filepath =~ /(19|20)\d\d\/[01]\d\/[0-3]\d\/[^\/]+$$/" 
-Exif:DateTimeOriginal"<${filepath;s/.*\/(\d+)\/(\d+)\/(\d+)\/.+$/$1:$2:$3 00:00:00/}" 
       -XMP:DateCreated"<${filepath;s/.*\/(\d+)\/(\d+)\/(\d+)\/.+$/$1:$2:$3 00:00:00/}" 
          -Exif:CreateDate"<${filepath;s/.*\/(\d+)\/(\d+)\/(\d+)\/.+$/$1:$2:$3 00:00:00/}" 
                     -Filename"<${filepath;s/.*\/(\d+)\/(\d+)\/(\d+)\/.+$/$1-$2-$3 /}${Filename;s/^\d{4}(-\d\d){2} (.+)/$2/}"  "FolderPath or ."

If you like to test the renames, it can be safe to...
exiftool -r -ext jpg -if "not ($XMP:DateCreated or $Exif:DateTimeOriginal or $Exif:CreateDate) and $filepath =~ /(19|20)\d\d\/[01]\d\/[0-3]\d\/[^\/]+$$/"  -TESTname"<${filepath;s/.*\/(\d+)\/(\d+)\/(\d+)\/.+$/$1-$2-$3 /}${Filename;s/^\d{4}(-\d\d){2} (.+)/$2/}"  "FolderPath or ."

If you like to experiment, showing "folder hierarchy created dates" in front of the filenames, it can be safe to ...
exiftool -r -ext jpg -if "not ($XMP:DateCreated or $Exif:DateTimeOriginal or $Exif:CreateDate) and $filepath =~ /(19|20)\d\d\/[01]\d\/[0-3]\d\/[^\/]+$$/" -p "${filepath;s/.*\/(\d+)\/(\d+)\/(\d+)\/.+$/$1:$2:$3 00:00:00\t/}   $filename" "FolderPath or ."
Windows8.1-64bit,  exiftool-v12.84(standalone),  sed-v4.0.7