if then else for date

Started by exifan, March 03, 2017, 02:41:00 PM

Previous topic - Next topic

exifan

Is there a way to construct an if-then-else condition? I am trying to name my images by date-time and would like to do that as follows:

IF (DateTimeOriginal) THEN
   filename--> formatted_DateTimeOriginal
ELSE IF  (CreateDate) THEN
   filename --> formatted_CreateDate
ELSE IF (ModifyDate) THEN
   filename --> formatted_ModifyDate
ELSE filename -->  formatted_file_DateTime.

Any guidance much appreciated. Thank you.

StarGeek

From the docs, note 1:

"Many tag values may be assigned in a single command. If two assignments affect the same tag, the latter takes precedence..."

So you do all these assigns in a single command and the last command that actually has a tag will take precedence.

Here's a recent example.
"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

exifan

@StarGeek. Thanks for that quick response. I had read that note and built the following command based on another thread in this forum:


    exiftool -P -m -progress -d '%Y%m%d_%H%M%S' \
    -ext jpg -ext JPG -ext png -ext PNG -ext BMP -ext bmp -ext gif -ext GIF -ext jpeg -ext tif \
   -ext mp4 -ext mpg -ext MPG -ext avi -ext AVI -ext wmf \
    '-filename<./dest/${FileModifyDate;s/(^\d{4}).*/$1/}/${FileModifyDate;s/(^\d{4})(\d{2}).*/$2/}/${FileModifyDate}_${ImageSize}_${Model;s/[^a-zA-Z\d\.\_]+/_/}%+.3c.%le' \
   '-filename<./dest/${ModifyDate;s/(^\d{4}).*/$1/}/${ModifyDate;s/(^\d{4})(\d{2}).*/$2/}/${ModifyDate}_${ImageSize}_${Model;s/[^a-zA-Z\d\.\_]+/_/}%+.3c.%le' \
   '-filename<./dest/${CreateDate;s/(^\d{4}).*/$1/}/${CreateDate;s/(^\d{4})(\d{2}).*/$2/}/${CreateDate}_${ImageSize}_${Model;s/[^a-zA-Z\d\.\_]+/_/}%+.3c.%le' \
   '-filename<./dest/${DateTimeOriginal;s/(^\d{4}).*/$1/}/${DateTimeOriginal;s/(^\d{4})(\d{2}).*/$2/}/${DateTimeOriginal}_${ImageSize}%+.3c.%le' \
   '-filename<./dest/${DateTimeOriginal;s/(^\d{4}).*/$1/}/${DateTimeOriginal;s/(^\d{4})(\d{2}).*/$2/}/${DateTimeOriginal}_${ImageSize}_${Model;s/[^a-zA-Z\d\.\_]+/_/}%+.3c.%le' \
   -r /mnt/remotedir/homePicts/ \
   -o ./dest/

but am still getting lots of files with names like  "_480x640_E800_000.jpg" when it should be based on the ModifyDate:
ExifToolVersion                 : 10.23
FileName                        : _480x640_E800_000.jpg
Directory                       : .
FileSize                        : 86 kB
FileModifyDate                  : 2013:06:03 16:46:23-05:00
FileAccessDate                  : 2017:01:08 20:44:59-06:00
FileInodeChangeDate             : 2017:03:02 00:42:58-06:00
FilePermissions                 : rw-rw-r--
FileType                        : JPEG
FileTypeExtension               : jpg
MIMEType                        : image/jpeg
JFIFVersion                     : 1.02
ExifByteOrder                   : Little-endian (Intel, II)
ImageDescription                :
Make                            : NIKON
Model                           : E800
Orientation                     : Horizontal (normalModifyDate                      : 2013:06:03 16:4)
XResolution                     : 300
YResolution                     : 300
ResolutionUnit                  : inches
Software                        : v984-75
6:23
YCbCrPositioning                : Co-sited
ExposureTime                    : 1/30
FNumber                         : 4.8
ExposureProgram                 : Program AE
ISO                             : 400
ExifVersion                     : 0210
DateTimeOriginal                : 0000:00:00 00:00:00
CreateDate                      : 0000:00:00 00:00:00

Any guidance what am I doing wrong?

Thanks again for the help.
Modify message

StarGeek

Quote from: exifan on March 03, 2017, 03:39:41 PM

Orientation                     : Horizontal (normalModifyDate                      : 2013:06:03 16:4)
...
Any guidance what am I doing wrong?

Is the output of exiftool -ModifyDate _480x640_E800_000.jpg actually 2013:06:03 16:4 (notice it's an incomplete timestamp)?  If so, it might not be something wrong with your command, it might be a problem with your metadata.  I can't replicate that number in ModifyDate, exiftool will correct it so I can't double check if that's where the actual problem is.

Also, you can add a filepath to the -d option.  Replace the repeated ./dest/${TAGNAME;s/(^\d{4}).*/$1/}/${TAGNAME;s/(^\d{4})(\d{2}).*/$2/}/${TAGNAME} with just ${TAGNAME} and use -d './dest/%Y/%m/%Y%m%d_%H%M%S".

For example, the first rename part would be
'-filename<${FileModifyDate}_${ImageSize}_${Model;s/[^a-zA-Z\d\.\_]+/_/}%+.3c.%le'
"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

exifan

The ModifyDate is correct (I messed up in the cut-paste last time)

Orientation                     : Horizontal (normal)
XResolution                     : 300
YResolution                     : 300
ResolutionUnit                  : inches
Software                        : v984-75
ModifyDate                      : 2013:06:03 16:46:23

I checked a few other files and all have either the ModifyDate or FileModifyDate present, but neither of those are showing up in the modified file name.

Thanks for also simplifying the command. Lot easier to read now :-)

StarGeek

Quote from: exifan on March 03, 2017, 06:25:38 PM
I checked a few other files and all have either the ModifyDate or FileModifyDate present, but neither of those are showing up in the modified file name.

What is the output of this command on one of those files?
exiftool -g1 -a -s -FileModifyDate -ModifyDate -CreateDate -DateTimeOriginal

"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

exifan

Here it is for a few different image files...

bert@picstore01:/media/SortedPictures$ exiftool -g1 -a -s -FileModifyDate -ModifyDate -CreateDate -DateTimeOriginal _480x640_E800_000.jpg
---- System ----
FileModifyDate                  : 2013:06:03 16:46:23-05:00
---- IFD0 ----
ModifyDate                      : 2013:06:03 16:46:23
---- ExifIFD ----
DateTimeOriginal                : 0000:00:00 00:00:00
CreateDate                      : 0000:00:00 00:00:00
---- XMP-xmp ----
ModifyDate                      : 2013:06:03 16:46:23-05:00
bert@picstore01:/media/SortedPictures$ exiftool -g1 -a -s -FileModifyDate -ModifyDate -CreateDate -DateTimeOriginal _640x480_E800_268.jpg
---- System ----
FileModifyDate                  : 2013:06:03 16:47:16-05:00
---- IFD0 ----
ModifyDate                      : 2013:06:03 16:47:16
---- ExifIFD ----
DateTimeOriginal                : 0000:00:00 00:00:00
CreateDate                      : 0000:00:00 00:00:00
---- XMP-xmp ----
ModifyDate                      : 2013:06:03 16:47:16-05:00
bert@picstore01:/media/SortedPictures$ exiftool -g1 -a -s -FileModifyDate -ModifyDate -CreateDate -DateTimeOriginal _640x480_E800_393.jpg
---- System ----
FileModifyDate                  : 2014:09:18 00:39:56-05:00
---- IFD0 ----
ModifyDate                      : 2014:09:18 00:39:56
---- ExifIFD ----
DateTimeOriginal                : 0000:00:00 00:00:00
CreateDate                      : 0000:00:00 00:00:00
---- XMP-xmp ----
ModifyDate                      : 2014:09:18 00:39:56-05:00

Thanks...

StarGeek

I had to test it but it's a problem with your DateTimeOriginal and CreateDate data.  Those tags exists so they override ModifyDate.  But since they're all 0 and it appears they aren't affected by the -d option in that case.
"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

Phil Harvey

Right.  The -d option doesn't work for date/time values which are outside the range supported by the system date/time functions.  Maybe something like this would do the trick:

   '-filename<./dest/${DateTimeOriginal;s/(^\d{4}).*/$1/}/${DateTimeOriginal;s/(^\d{4})(\d{2}).*/$2/}/${DateTimeOriginal;$_ = undef if /^0000/}_${ImageSize}_${Model;s/[^a-zA-Z\d\.\_]+/_/}%+.3c.%le' \

Here I have set one of the DateTimeOriginal values to "undef" if the value begins with "0000".

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

exifan

Hi Phil, Thought that was a brilliant solution, but sadly no success. Still the same results as before. I am using the following command:

exiftool -P -m -progress -d './testOut/%Y/%m/%Y%m%d_%H%M%S' \
    -ext jpg -ext JPG -ext png -ext PNG -ext BMP -ext bmp -ext gif -ext GIF -ext jpeg -ext tif \
    -ext mp4 -ext mpg -ext MPG -ext avi -ext AVI -ext wmf \
    '-filename<${FileModifyDate;$_ = undef if /^0000/}_${ImageSize}_${Model;s/[^a-zA-Z\d\.\_]+/_/}%+.3c.%le' \
    '-filename<${ModifyDate;$_ = undef if /^0000/}_${ImageSize}_${Model;s/[^a-zA-Z\d\.\_]+/_/}%+.3c.%le' \
    '-filename<${CreateDate;$_ = undef if /^0000/}_${ImageSize}_${Model;s/[^a-zA-Z\d\.\_]+/_/}%+.3c.%le' \
    '-filename<${DateTimeOriginal;$_ = undef if /^0000/}_${ImageSize}_${Model;s/[^a-zA-Z\d\.\_]+/_/}%+.3c.%le' \
    -r ./testIn  \
    -o ./testOut

Many thanks to you and StarGeek for help. I am hopeful still that a solution will emerge.


Phil Harvey

You need to drop the -m option.

            If a specified tag does not exist, a minor warning is issued and
            the line with the missing tag is not printed.  However, the -f
            option may be used to set the value of missing tags to '-' (but
            this may be configured via the MissingTagValue API option), or the
            -m option may be used to ignore minor warnings and leave the
            missing values empty
.


Note that this documentation may be a bit confusing because it is in the context of the -p option, but the syntax is the same and setting the value to undef is the same as a missing tag.  So the -f and -m options are incompatible with my suggestion.

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

exifan

Ah! works like a charm now. I had browsed through that section of the documentation but did not make the connection with -m. Thank you for the clarification and help.

If I may impose on your generosity with one more question! Can I put multiple "If" conditions in that construct? Just curious.

Many thanks,

Phil Harvey

Great.

You can always put multiple -if conditions in a command -- a file will be processed if the logical "and" of all the conditions is true.

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