Author Topic: How to change exif dates to match the file name?  (Read 17507 times)

5267

  • Newbie
  • *
  • Posts: 2
How to change exif dates to match the file name?
« on: July 21, 2016, 08:14:43 AM »
Hi all, you can probably guess I'm pretty new to exiftool. :D For some reason, I just can't get my head around it, but it seems like this is the only application that will perform the task I need. My problem is this:

All my image files follow the same file name pattern 'YY-MM-DD HHMMSS'. For example, for a photo taken today at 13:50 the file would be named '16-07-21 135032'. All the dates on the file names are correct but the exif capture date is not. If possible, I would like all dates shown on the exif data including the created and modified dates to be the same. So I was wondering, would there be a way to change the dates shown in exif data to match the file name?

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 14759
    • ExifTool Home Page
Re: How to change exif dates to match the file name?
« Reply #1 on: July 21, 2016, 11:06:59 AM »
You can use this command to set the common EXIF date/times from your file names:

exiftool "-alldates<20$filename" DIR

This should work assuming that all years are 2000 or later.  Because you only have 2 digits in your year, I have added "20" before the file name to complete the 4-digit year.

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

5267

  • Newbie
  • *
  • Posts: 2
Re: How to change exif dates to match the file name?
« Reply #2 on: July 21, 2016, 01:10:21 PM »
That works perfectly, thanks a lot Phil. I was just missing the '20'. ;D

RRRichard

  • Newbie
  • *
  • Posts: 7
Re: How to change exif dates to match the file name?
« Reply #3 on: November 08, 2016, 08:07:43 PM »
I'm new here also - but this post is the closest solution I've found for what I would like to do with ExifTool... EXCITING!

I catalog all my photo files inside a folder named by date taken (YYYY-MM-DD-description). I would like to set / create all EXIF dates using the date in the foldername. Sometimes the EXIF dates match but sometimes scanned photos may have inaccurate EXIF dates or no EXIF dates at all. Additionally, some photo editors may end up stripping the EXIF data. Can ExifTool accomplish this on a batch basis for all files inside a folder?

It would be a bonus if ExifTool could also set all the file dates (non EXIF) as well. (Bonus??? Hmmm... maybe like a dream come true!)

Why do this? Well I'm exploring photo storage in the cloud and it would be nice if the photos were automatically sorted by date in the cloud. It seems that there is no consistency across cloud solutions as to what date is used for sorting. If all dates (file & EXIF) were the same then sorting should be correct in any cloud.

The bulk of my files are JPG and it seems that ExifTool has no problem targeting those files. What happens with MOV or MP4 video files or with PNG files?

Any assistance towards a solution would really be appreciated!

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 14759
    • ExifTool Home Page
Re: How to change exif dates to match the file name?
« Reply #4 on: November 09, 2016, 07:13:48 AM »
Sure,  a command like this may do what you want:

exiftool "-alldates<${directory}${filemodifydate;s/.* //}" "-alldates<${directory}${modifydate;s/.* //}" "-alldates<${directory}${createdate;s/.* //}" "-alldates<${directory}${datetimeoriginal;s/.* //}" -r DIR

Where DIR is the directory containing all of the subdirectories named by date.

The command is so long because I have tried to fill in the time with any other available time, in order of precedence: 1) DateTimeOriginal, 2) CreateDate, 3) ModifyDate, 4) FileModifyDate.

If you just want to use a fixed time instead, the command would be a lot simpler:

exiftool "-alldates<$directory 00:00:00" -r DIR

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

RRRichard

  • Newbie
  • *
  • Posts: 7
Re: How to change exif dates to match the file name?
« Reply #5 on: November 09, 2016, 12:06:18 PM »
Phil,

Thank you so much for your reply! This certainly gets me started on using ExifTools - your short command line seems to do the job - VERY impressive! As for the long command line, well, that will be a challenge for any newbie to decipher! :o)

I will do some digging in your documentation but I think I should be able to reset the OS file dates as well (I see alldates does the EXIF dates only). Perhaps I will challenge myself to define a new custom tag which includes EXIF and OS dates in one tag name! Can you point me to any documentation that lists value names (i.e. $directory, $filename, etc) and tag names (i.e. alldates, filemodifydate, etc)?

Thanks again Phil - for your reply and for your software!



Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 14759
    • ExifTool Home Page
Re: How to change exif dates to match the file name?
« Reply #6 on: November 09, 2016, 12:13:43 PM »
Directory, FileName, FileModifyDate, are all tag names (although these are special "pseudo" system tags).  The complete tag name documentation is here, but specifically it is the Extra Tags that you are mainly interested in.  AllDates is a Shortcut Tag.  Also see the sample config file if you want to define your own shortcuts.

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

RRRichard

  • Newbie
  • *
  • Posts: 7
Re: How to change exif dates to match the file name?
« Reply #7 on: November 10, 2016, 01:05:45 AM »
Great reference material! This should get me going.

Thanks again for taking the time to assist yet another newbie - this little bit of help made a big difference in getting started with ExifTool!

Richard

RRRichard

  • Newbie
  • *
  • Posts: 7
Re: How to change exif dates to match the file name?
« Reply #8 on: November 11, 2016, 06:04:01 PM »
Phil,

I seem to be having a problem with the file directory structure interfering with what I am trying to accomplish and wonder if there is a way around the problem.

I use a directory structure like this: Photos\YYYY\YYYY-MM\YYYY-MM-DD with photos being stored in the lowest folders only.

When I try to run the command: exiftools "-alldates<$directory 00:00:00" -r C:\Photos

I'm getting incorrect dates... it seems the date is being set from the full directory name instead of the immediate directory name for each file. Is there a way to force ExifTool to use the immediate directory name only? (I'm even getting illegal dates written with months >12). Do I need to run the command in each folder holding the files instead of in the folders in higher levels? (That would be a lot more work.)

Additionally, when I try running: exiftools "-FileModifyDate<$directory 00:00:00" -r C:\Photos

I get a warning about no writable tags but the list of tags indicates that FileModifyDate is writable. I have used an administrator level command window.

Clearly, I have some work to do before "earning my wings" for ExifTool...

StarGeek

  • Global Moderator
  • ExifTool Freak
  • *****
  • Posts: 2516
Re: How to change exif dates to match the file name?
« Reply #9 on: November 11, 2016, 06:55:20 PM »
Try using :
exiftools "-alldates<${directory;s/.*\/(.*)/$1/} 00:00:00" -r C:\Photos
Troubleshooting hints:
* When posting, include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).
* Double all percent signs (%) in a Windows batch file.
* If your GPS coords are negative, make sure and set the GpsLatitudeRef and GpsLongitudeRef tags correctly.

RRRichard

  • Newbie
  • *
  • Posts: 7
Re: How to change exif dates to match the file name?
« Reply #10 on: November 11, 2016, 07:27:46 PM »
StarGeek,

Thank you so much for your solution! The one command changed the EXIF dates flawlessly across 147 directories and 2,158 image files... WOW!

I retried it for the FileModifyDate and FileCreateDate tags and, for the most part, those tags were also changed correctly. I did notice a minor glitch when I moved from the 2013-03-09 folder to the 2013-03-12 folder. The EXIF dates were consistent all the way through but the file dates started to get tagged one hour less than the directory date (i.e. 2013-03-11 23:00:00 instead of 2013-03-12 00:00:00) - kind of weird. This happened to all the later dates as well. Not really a big deal for me - just strange.

Would you be able to point me to some documentation that explains at how you arrived at "{directory;s/.*\/(.*)/$1/}" instead of "directory"? I would love to understand this.

BTW, my typos in my previous message... "exiftools" should be "exiftool" of course.

THANKS again!

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 14759
    • ExifTool Home Page
Re: How to change exif dates to match the file name?
« Reply #11 on: November 12, 2016, 08:09:33 AM »
the file dates started to get tagged one hour less than the directory date (i.e. 2013-03-11 23:00:00 instead of 2013-03-12 00:00:00) - kind of weird.

This is likely somehow related to the known Windows time zone bug.  Windows doesn't treat time zones properly, and it causes all sorts of problems like this.

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

RRRichard

  • Newbie
  • *
  • Posts: 7
Re: How to change exif dates to match the file name?
« Reply #12 on: November 12, 2016, 11:24:38 AM »
Thanks for the help here to solve my problem. Looks like I might have to get my Perl wings alongside getting my ExifTool wings (re. StarGeek's solution).  :D

StarGeek

  • Global Moderator
  • ExifTool Freak
  • *****
  • Posts: 2516
Re: How to change exif dates to match the file name?
« Reply #13 on: November 12, 2016, 02:49:24 PM »
Would you be able to point me to some documentation that explains at how you arrived at "{directory;s/.*\/(.*)/$1/}" instead of "directory"? I would love to understand this.

Phil calls it "Advanced Formatting" and you can find a little info under the -p option docs.  It's basically putting some perl code, often perl regular expression (regex) substitution, into the command line.  Teaching perl and regex is a bit beyond the scope of this forum, but there are plenty of resources out on the web.  In most cases, problems can be solved with just knowing some regex.

My suggested resources for learning regex are Regular-Expressions.info for the basics and RegEx101 for testing expressions out.  One of the nice things about RegEx101 is that it'll give you a step by step explanation in the upper right corner.  For example, here's the regex I used. 
Troubleshooting hints:
* When posting, include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).
* Double all percent signs (%) in a Windows batch file.
* If your GPS coords are negative, make sure and set the GpsLatitudeRef and GpsLongitudeRef tags correctly.

RRRichard

  • Newbie
  • *
  • Posts: 7
Re: How to change exif dates to match the file name?
« Reply #14 on: November 15, 2016, 11:32:44 PM »
StarGeek,

That's perfect - it should get me started - much appreciated!

Thanks again,
Richard