use filename as DateTimeOriginal

Started by schimmey, January 10, 2016, 01:11:12 PM

Previous topic - Next topic

schimmey

Hi All,

I name my scanned image files in date taken. For a photograph taken on 01/01/2010 I name 20100101-001 where the 001 denotes the sequence number of images taken that day (001 being the first).

Is there any way in exiftool that I can use the file name to write the DateTimeOriginal. Something like "=filename" (although I realise I need to indicate yyyy:mm:dd) etc.

Thanks,

Paul

Phil Harvey

Hi Paul,

If the name also included the time, then you could simply do this:

exiftool "-datetimeoriginal<filename" DIR

But as it is, you need to do something more complex to remove the sequence number and add a time:

exiftool "-datetimeoriginal<${filename;s/-.*//} 00:00:00" DIR

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

schimmey

Hi Phil,

Thanks for that. I have tried it with one file and has worked. Will do a batch tomorrow. Is it possible you can take me through the code, or point me in the right direction?

Paul

Phil Harvey

The basic syntax is explained in the "advanced formatting feature" part of the -tagsFromFile section of the exiftool application documentation.  The "s/-.*//" part is a Perl substitution expression that deletes everything after and including the first "-" in the FileName string.

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

Hiryu

Hey Phil,

Been experimenting a lot and think I finally figured out a solution to my previous issue.  It had to do with not understanding perl substitutions -- gotta read up on that more.  I'm gonna piggy back off this discussion, because it ultimately lead me to my solution -- but wanted to understand more.

The "s/-.*//" part is a Perl substitution expression that deletes everything after and including the first "-" in the FileName string.

How do I write a Perl substitution expression that deletes everything BEFORE the "-", instead of after.

Is there a good resource for a newbie on learning the syntax for these perl substitutions such that I can better write commands with exif in advanced format?  Thanks.

Hayo Baan

That would be s/[^-]*-//. The [^-]* selects all characters up to but not including a -, .* can be used too, but as that eats a - too, the replacement would be all characters up the the last -, not the first.
Hayo Baan – Photography
Web: www.hayobaan.nl

Hiryu

Hey -- thanks so much.  I actually figured it out before I came and saw the reply to the post.  I ended up using '-author<${filename;s/-.*//}'  and that worked out!  Finally got somewhere with my command.

Now the final piece I'm struggling with is actually renaming a file with exiftool.

I have a bunch of files named:

xadfd - 9 Jan 2015
yeoiu - 17 February 2015
hgaoiadfa - 17 March - 09 April 2015

What I want to do is rename the file so that the beginning is all standard (instead of xadfd, yeoiu, hgaoiadfa, etc.)  I want to rename them with the structure

Term Report -  9 Jan 2015
Term Report - 17 February 2015
Term Report  - 17 March - 09 April 2015

How would I go about doing this?  Can I strip the date substring from the original file?  The dates aren't standard (some have the month spelled out, some don't) so I figure I have to just grab everything from after the "-"


I tried the following but am getting a substitution error
exiftool -filename='Term Report - <$filename;s/-.*//'.pdf *.pdf

Could you let me know how I can write the command so that there is a hard-coded element (Term Report - ) and add <$filename;s/-.*//' string so that the result is

Term Report (string from after the "-" = Term Report - 9 Jan 2015 

???

Thanks so much!

Hayo Baan

Use the following command:
exiftool -filename'<${filename;s/^[^-]*/Term Report /}' *.pdf
(the first part between the // is the search pattern, the second part between the // is the replacement pattern, the ^[^-]* matches all characters from the start of the string until it finds a - so make sure your files do contain a - or otherwise you would end up with a file called just "Term Report ").

Cheers,
Hayo
Hayo Baan – Photography
Web: www.hayobaan.nl

Hiryu

Thank you so much!

That seems to work a treat -- will have to test out on more files.  There are a lot of files I have to process and some I see are

hgaoiadfa - 17 March - 09 April 2015 TRS
afoeifu - 17 March - 09 April 2014 gfd

What if I want to include in the command getting rid of anything that is after 20XX?  Is that even possible?  as you see some of them are 2015, some 2014, etc.

I'm gonna experiment with using a separate exiftool command after the one you showed me, but was wondering if there was a more elegant way.



Hayo Baan

Sure that's possible. Regular expressions are extremely powerful and what we are doing here really just the simple stuff. Anyway to throw away everything after the year and still do the prefix, you could so something like this:
exiftool -filename'<${filename;s/^[^-]*(.* 20[0-9][0-9]).*/Term Report $1/}' *.pdf
There ar different ways of doing this, though, especially if not all filenames have a year. I suggest you do some reading on regular expressions and try to work out some alternatives yourself :)
Hayo Baan – Photography
Web: www.hayobaan.nl

Hiryu

Thank you greatly -- I definitely will do more reading on regular expressions -- but still really appreciate all of your help!

Hiryu

Hey Hayo,

Thanks for all the help -- trying to learn and understand the syntax for regular expression

exiftool -filename'<${filename;s/^[^-]*(.* 20[0-9][0-9]).*/Term Report $1/}' *.pdf

If I were to write it as

exiftool -filename'<${filename;s/^[^-]*(.* 20[0-9][0-9]).*/$1 - Term Report/}' *.pdf

Would I get 17 March - 09 April 2015 - Term Report ?

for the file hgaoiadfa - 17 March - 09 April 2015 TRS

Meaning, how would you change the command so that the filename changes from Term Report - dates to Dates - Term Report?
Just for my edification so that I can make sense of the command.  I think if I saw the difference I could decipher the syntax better.


Thank you -- you've been a great great help.

Hayo Baan

You almost got it! The problem is that the capture group also captures the - so your file will have a - in front. With a small change, also getting rid of any white space surrounding the dash, you're there:
exiftool -filename'<${filename;s/^[^-]*-\s*(.* 20[0-9][0-9]).*/$1 - Term Report/}' *.pdf

Cheers,
Hayo
Hayo Baan – Photography
Web: www.hayobaan.nl

Hiryu

Thanks Hayo -- you are a godsend.  Much appreciation for your help!

StarGeek

Quote from: Hiryu on January 18, 2016, 05:45:38 PM
Thank you greatly -- I definitely will do more reading on regular expressions -- but still really appreciate all of your help!

Regular-Expressions.info is the site where I learned the most about regex, though there are plenty of other sites like it out there.  You can test out your regex patterns on a site like RegEx101, which can break down and explain individual parts of the regex pattern.
"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