If-condition based on Create Date, but for time (hour) only?

Started by Espen, August 19, 2014, 04:57:02 PM

Previous topic - Next topic

Espen

Hi!  :)

I've tried to study the documentation regarding "if" conditions, and found the following example:

# add one hour to all images created on or after Apr. 2, 2006
    exiftool -alldates+=1 -if '$CreateDate ge "2006:04:02"' dir

Still, I need some help.

I have a directory, "A" with a lot of images taken during the summer months (time lapses).
I would like to move all images taken in the evening/night (e.g. from 17:00 - 06:00, ignoring month/day) to another directory, "B".

I was thinking I could use the CreateDate tag and somehow skip the year/month/day and only set the time/hour as a condition.

Would that be possible?

Regards,

Espen

StarGeek

Try this command (swap single and double quotes if not on windows machine):

exiftool -d "%H" -if "$CreateDate le '6' or $CreateDate ge '17'"  -Directory=/path/to/B DirA

The -d "%H" option changes the format for date/time tags, in this case showing only the Hour.

Make sure and test the command first, as I haven't tested it.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Phil Harvey

Quote from: StarGeek on August 19, 2014, 06:34:06 PM
Try this command (swap single and double quotes if not on windows machine):

exiftool -d "%H" -if "$CreateDate le '6' or $CreateDate ge '17'"  -Directory=/path/to/B DirA

Smart.

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

Espen

Quote from: StarGeek on August 19, 2014, 06:34:06 PM
Try this command (swap single and double quotes if not on windows machine):

exiftool -d "%H" -if "$CreateDate le '6' or $CreateDate ge '17'"  -Directory=/path/to/B DirA

The -d "%H" option changes the format for date/time tags, in this case showing only the Hour.

Make sure and test the command first, as I haven't tested it.

Thank you very much for your help! I have been out of office all day/night, so haven't had the time to test your suggested command yet, but I will do that tomorrow and report back.

I must say I'm really impressed with the possibilities and flexibility of ExifTool! Well done, Phil Harvey!

Espen

Ok, I did the test at home with some random images taken at different hours.

I had to use the and-operator instead of or to get it to work:

exiftool -d "%H" -if "$CreateDate le '6' and $CreateDate ge '17'"  -Directory=/path/to/B DirA

With the or-operator, all images were moved to DirB?! I can't really understand why that would happen? Do the two conditions in conjunction with or somehow "count past" each other?  ???

StarGeek

Are you sure using "and" actually worked?  I did some actual testing now and using "and" only picked out the ones that had a CreateDate greater than or equal to 17.

Ok, this seems to work for me. 
exiftool -d "%H" -if "$CreateDate<=6 or $CreateDate>=17" -Directory=/path/to/B DirA

I thought that I read that le/ge should be the same as <=/>= but maybe I'm wrong?  It looks like a string vs. numeric comparison problem.

One more thing that might pop up, though it shouldn't from the sound of your data source, would be files that don't have a CreateDate set.  If that's the case then you'd have to set a check for that.

exiftool -d "%H" -if "($CreateDate<=6 or $CreateDate>=17) and defined $CreateDate" -Directory=/path/to/B DirA

Edit:  Yep, looks like my mistake.  le/ge are string operands, <=/>= are numeric. 
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Phil Harvey

Quote from: StarGeek on August 20, 2014, 08:10:36 PM
exiftool -d "%H" -if "($CreateDate<=6 or $CreateDate>=17) and defined $CreateDate" -Directory=/path/to/B DirA

Edit:  Yep, looks like my mistake.  le/ge are string operands, <=/>= are numeric.

Right.  I should have caught the string comparison.  You don't need to check to see if CreateDate is defined, because a condition will fail on any type of error like 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 ($).

Espen

Quote from: StarGeek on August 20, 2014, 08:10:36 PM
Are you sure using "and" actually worked?  I did some actual testing now and using "and" only picked out the ones that had a CreateDate greater than or equal to 17.
You're completely right! I misinterpreted the results when using "and".

Quote from: StarGeek on August 20, 2014, 08:10:36 PM
Ok, this seems to work for me.
exiftool -d "%H" -if "$CreateDate<=6 or $CreateDate>=17" -Directory=/path/to/B DirA
I can confirm that using numeric operands works perfectly!

StarGeek

Quote from: Phil Harvey on August 21, 2014, 07:18:23 AMYou don't need to check to see if CreateDate is defined, because a condition will fail on any type of error like this.

While I didn't use a move operation for testing, when I did this:
exiftool -d "%H" -if "($CreateDate<=6 or $CreateDate>=17)" -createdate -filename X:\!temp\t*
this was the output
======== X:/!temp/temp - after.csv
File Name                       : temp - after.csv
======== X:/!temp/temp - before.csv
File Name                       : temp - before.csv
======== X:/!temp/temp.txt
File Name                       : temp.txt
======== X:/!temp/Test.jpg
Create Date                     : 2012:08:30 22:25:33
File Name                       : Test.jpg
======== X:/!temp/Test.jpg_original
Create Date                     : 2012:08:30 22:25:33
File Name                       : Test.jpg_original
======== X:/!temp/Test2.jpg
Create Date                     : 2012:08:30 22:25:33
File Name                       : Test2.jpg
======== X:/!temp/tif.jpg
File Name                       : tif.jpg
======== X:/!temp/tif.jpg_original
File Name                       : tif.jpg_original
    8 image files read


As you can see, it was evaluated as true for files that didn't have a CreateDate.  I followed up with
exiftool -d "%H" -if "($CreateDate==0)" -createdate -filename X:\!temp\t*
which listed all the files that didn't have a CreateDate
======== X:/!temp/temp - after.csv
File Name                       : temp - after.csv
======== X:/!temp/temp - before.csv
File Name                       : temp - before.csv
======== X:/!temp/temp.txt
File Name                       : temp.txt
======== X:/!temp/tif.jpg
File Name                       : tif.jpg
======== X:/!temp/tif.jpg_original
File Name                       : tif.jpg_original
    3 files failed condition
    5 image files read


So it seems that the undefined CreateDate is evaluating as 0.

* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Phil Harvey

Quote from: StarGeek on August 21, 2014, 03:16:59 PM
So it seems that the undefined CreateDate is evaluating as 0.

That's crazy.  I don't understand your result.  Here is what I get (on my Mac):

> exiftool -createdate tmp
======== tmp/a.jpg
======== tmp/b.jpg
======== tmp/c.jpg
======== tmp/d.jpg
Create Date                     : 2001:05:19 18:36:41
    1 directories scanned
    4 image files read

> exiftool -d "%H" -if '($CreateDate<=6 or $CreateDate>=17)' -createdate tmp
======== tmp/d.jpg
Create Date                     : 18
    1 directories scanned
    3 files failed condition
    1 image files read


I don't see how this could be operating-system dependent, but I'll test on Windows when I get a chance.

- Phil

Edit:  Wait.  Your output doesn't correspond with the command you gave because your CreateDate is not formatted by the -d option.
...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 ($).

StarGeek

Quote from: Phil Harvey on August 21, 2014, 03:37:22 PM
Edit:  Wait.  Your output doesn't correspond with the command you gave because your CreateDate is not formatted by the -d option.

Oops.... Copy/paste mistake I think.  Let me double check.  Here's the full output when rerun it:
C:\Windows\System32>exiftool -createdate X:\!temp\t*
======== X:/!temp/temp - after.csv
Error: Unknown file type - X:/!temp/temp - after.csv
======== X:/!temp/temp - before.csv
Error: Unknown file type - X:/!temp/temp - before.csv
======== X:/!temp/temp.txt
Error: Unknown file type - X:/!temp/temp.txt
======== X:/!temp/Test.jpg
Create Date                     : 2012:08:30 22:25:33
======== X:/!temp/Test.jpg_original
Create Date                     : 2012:08:30 22:25:33
======== X:/!temp/Test2.jpg
Create Date                     : 2012:08:30 22:25:33
======== X:/!temp/tif.jpg
======== X:/!temp/tif.jpg_original
    8 image files read

C:\Windows\System32>exiftool -d "%H" -if "($CreateDate<=6 or $CreateDate>=17)" -createdate -filename X:\!temp\t*
======== X:/!temp/temp - after.csv
File Name                       : temp - after.csv
======== X:/!temp/temp - before.csv
File Name                       : temp - before.csv
======== X:/!temp/temp.txt
File Name                       : temp.txt
======== X:/!temp/Test.jpg
Create Date                     : 22
File Name                       : Test.jpg
======== X:/!temp/Test.jpg_original
Create Date                     : 22
File Name                       : Test.jpg_original
======== X:/!temp/Test2.jpg
Create Date                     : 22
File Name                       : Test2.jpg
======== X:/!temp/tif.jpg
File Name                       : tif.jpg
======== X:/!temp/tif.jpg_original
File Name                       : tif.jpg_original
    8 image files read


Additionally, I renamed my ".ExifTool_config" just in case I had something stupid messing it up.  This is using v9.67.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Phil Harvey

StarGeek,

Sorry for the delay in responding.  I forgot to get back to this after my vacation.

I have run some tests in Windows, and you are right.  For some reason, an undefined tag tests with numeric operators as if it has a value of 0.  But this is only when running the Windows exe.  If I run the Perl version (with Perl 5.8.7 -- the same one that is bundled in the exe), I get different results. Here is a cmd.exe session to illustrate this:

> exiftool.exe -if "$createdate<=6 or $createdate>=17" -filename -createdate a.jpg
File Name                       : a.jpg

> perl exiftool -if "$createdate<=6 or $createdate>=17" -filename -createdate a.jpg
    1 files failed condition


Weird.

So when using the Windows exe version you need to test to make sure the tag is defined.

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