Changing date based on filename

Started by Paulo.Tebet, November 17, 2010, 03:46:31 AM

Previous topic - Next topic

Paulo.Tebet

Hi All,

I am very new to the exiftool and I am trying to accomplish the following task:

I would like to be able to change the exif metadata of Date Picture Taken (or whatever is the official name for this metadata) based on the content of the file name. Somehow I have already managed to rename my files based on the date it was supposedly taken. But now I am finding out that some of these files does not have a correct exif date or doesn't have an exif date at all. So I would like to extract the date from the file name and edit it in the exif.

For example. I have a file name "2005 05 15_5678" with YYYY MM  DD_xxxx where xxxx is just a sequential number. I would like to have a command line in exiftool that would take the year, month and day from the file name based on the position of the characters.

So it should take from character 1 through 4 of the file name (2005) and use it as the YEAR, from 6 through 7 (05) and use it as the MONTH, from 09 through 10 (15) and use it as the DAY and since I don't have the HH:MM:SS in the file name, especify it in the command line. Could be 00:00:00 or will exif assume some time information if none is suplied?

Any idea how could this be accomplished?

Thanks very much in advance.

Phil Harvey

I think that reading this thread may help.

Let me know if you have any questions after reading this.

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

Paulo.Tebet

Hi Phil,

Thanks for taking the time to answer my question.

I have read all the threads you've mention but I still have some questions:


About this code:
%Image::ExifTool::UserDefined = (
'Image::ExifTool::Composite' => {
MyDateTime2 => {
Require => 'FileName',
ValueConv => q[
return undef unless $val =~ /^(\d{6})p-*(\d{4})/;
return $1 lt '70' ? "20$1 $2" : "19$1 $2";
],
},
},
);
1; #end


What language is this?

I undertand this code is to handle filenames in the format of YYMMDDp-HHMM and that this is defined in this part of the code:

return undef unless $val =~ /^(\d{6})p-*(\d{4})/;

So im my case since I have dates in the form of "YYYY MM DD_xxxx" I would have to change that code

Here what I have in mind, even though I am pretty sure I am missing something:

%Image::ExifTool::UserDefined = (
"Image::ExifTool::Composite" => {
MyDateTime2 => {
Require => "FileName",
ValueConv => q[
return undef unless $val =~ /^(\d{4}) *(\d{2}) *(\d{2}) *(\d{4})/;
return "$1$2$3 000000";
],
},
},
);
1; #end



I didn't know how to handle the spaces in my file names, so I try to guess something on the "return undef unless" part of the code. But this I can even live without because I can guarantee my files will conform to that standard. The big problem is the Return "$1 000000"; part of it.

I don't understand where is it coming from or what is this $1 part.

I thought that the YYYY would be $1 and MM $2 and DD $3 so I made changes to my code as shown above.

I then gave it a try and this is what I've got:


imac-de-beto:Volumes beto$ exiftool "-datetimeoriginal<mydatetime" My\ Book\ 1/FAV\ Utils/FixExifDate
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7233.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7233.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7234.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7234.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7235.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7235.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7236.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7236.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7237.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7237.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7238.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7238.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7239.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7239.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7240.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7240.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7241.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7241.JPG
    1 directories scanned
    0 image files updated
    9 image files unchanged

Paulo.Tebet

I have just realized that in the Config file I was using MyDateTime2 and in the command line mydatetime. I have already fixed this to be the same (not sure if it is case sensitive). I am using MyDateTime in both the config file and the command line but still couldn't get it to work.

This is the output:


imac-de-beto:Volumes beto$ exiftool "-datetimeoriginal<MyDateTime" My\ Book\ 1/FAV\ Utils/FixExifDate
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7233.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7233.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7234.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7234.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7235.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7235.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7236.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7236.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7237.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7237.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7238.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7238.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7239.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7239.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7240.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7240.JPG
Warning: Unknown format (14640) for IFD0 tag 0x3032 - My Book 1/FAV Utils/FixExifDate/2005 06 25_7241.JPG
Warning: No writable tags found - My Book 1/FAV Utils/FixExifDate/2005 06 25_7241.JPG
    1 directories scanned
    0 image files updated
    9 image files unchanged

Phil Harvey

Hi Paulo,

Quote from: Paulo.Tebet on November 17, 2010, 01:47:04 PM
What language is this?

Perl.  More specifically, I think it is the Perl regular expressions (the =~ operator) that you are having trouble with.  These regular expressions are also used in the C regex functions, and by other utilities such as awk.

Quote
return undef unless $val =~ /^(\d{4}) *(\d{2}) *(\d{2}) *(\d{4})/;
return "$1$2$3 000000";

the " *" represents "any number of spaces".  So for the 3rd one I think you want "_" instead, since there is an underline before the last 4 digits.

Quote
I don't understand where is it coming from or what is this $1 part.

The values of the variables $1, $2, $3 etc are taken from the strings inside the 1st, 2nd, 3rd, etc sets of brackets from the most recent =~ operation.

Quote
I thought that the YYYY would be $1 and MM $2 and DD $3 so I made changes to my code as shown above.

Very good. :)

I think your only problem was that the last " *" should be changed to "_".

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

Paulo.Tebet


Phil Harvey

I should have paid closer attention to your warnings.  Even after you write the date/time tag properly you will get write errors due to this:

Warning: Unknown format (14640) for IFD0 tag 0x3032

And since all of your images seem to have this problem I would guess that you have processed them all with the same buggy software.  You will have to fix this problem before you will be able to write DateTimeOriginal with ExifTool.  See FAQ number 20 for help with this.

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