Regular Expression to fomat decade from date?

Started by willuwe, April 29, 2020, 01:55:26 AM

Previous topic - Next topic

willuwe

Hi team,

I saw the post where Phil showed the way to using a stack of -if conditions to define 10-year brackets of date (taken or created...) and then set the directories according to 1960s, 1970s, 1980s etc.

I find organizing files by decades is very useful.

Is it possible to directly format the output directories by using a string handling command/regular expression, which would generate output like "YYY\YYYY\YY-MM\", i.e. "cutting the first three digits from the 4-digit year, thus indicating the decade? It might yield a much shorter command to solve this task.

Thanks,

Uwe

StarGeek

You could use something like this
exiftool -d "%Y/%y-%m/%%F" "-testname<${DateTimeOriginal#;DateFmt('%Y');s/\d$//}/$DateTimeOriginal" /path/to/files/

Which has these results
C:\>exiftool -g1 -a -s -DateTimeOriginal y:\!temp\Test3.jpg y:\!temp\Test4.jpg
======== y:/!temp/Test3.jpg
---- ExifIFD ----
DateTimeOriginal                : 1975:09:22 11:53:22
======== y:/!temp/Test4.jpg
---- ExifIFD ----
DateTimeOriginal                : 2020:04:28 23:49:51
    2 image files read

C:\>exiftool -d "%Y/%y-%m/%%F" "-testname<${DateTimeOriginal#;DateFmt('%Y');s/\d$//}/$DateTimeOriginal" y:\!temp\Test3.jpg  y:\!temp\Test4.jpg
'y:/!temp/Test3.jpg' --> '197/1975/75-09/Test3.jpg'
'y:/!temp/Test4.jpg' --> '202/2020/20-04/Test4.jpg'
    0 image files updated
    2 image files unchanged


But it can be set to do the full decade with a zero in the right spot

C:\>exiftool -d "%Y/%y-%m/%%F" "-testname<${DateTimeOriginal#;DateFmt('%Y');s/\d$//}0/$DateTimeOriginal" y:\!temp\Test3.jpg  y:\!temp\Test4.jpg
'y:/!temp/Test3.jpg' --> '1970/1975/75-09/Test3.jpg'
'y:/!temp/Test4.jpg' --> '2020/2020/20-04/Test4.jpg'
    0 image files updated
"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

willuwe

Fantastic. This does the trick - many thanks!

Uwe

Phil Harvey

@StarGeek: Interesting that you did this

${DateTimeOriginal#;DateFmt('%Y');s/\d$//}0

instead of this

${DateTimeOriginal#;DateFmt('%Y');s/\d$/0/}

... thinking outside of the box I guess. ;)

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

StarGeek

Well, yeah, there is that :D

Actually, it was a last second thought just before I posted.  Popped it in, posted, and went to bed.
"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

RadOD

Help!

/mnt/user/Downloads/Image-ExifTool-12.25# ./exiftool -g1 -a -s -DateTimeOriginal     /mnt/user/Photos/_Archive/2010/2010-03/2010-03-01-174249.jpg
---- ExifIFD ----
DateTimeOriginal                : 2010:03:01 17:42:49


but...

/mnt/user/Downloads/Image-ExifTool-12.25# ./exiftool -d "%Y/%y-%m/%%F" "-testname<${DateTimeOriginal#;DateFmt('%Y');s/\d$//}0/$DateTimeOriginal" /mnt/user/Photos/_Archive/2010/2010-03/2010-03-01-174249.jpg
Warning: No writable tags set from /mnt/user/Photos/_Archive/2010/2010-03/2010-03-01-174249.jpg
Warning: Invalid tag name '0/' - /mnt/user/Photos/_Archive/2010/2010-03/2010-03-01-174249.jpg
    0 image files updated
    1 image files unchanged


or...

/mnt/user/Downloads/Image-ExifTool-12.25# ./exiftool -d "%Y/%y-%m/%%F" "-testname<${DateTimeOriginal#;DateFmt('%Y');s/\d$/0/}/$DateTimeOriginal" /mnt/user/Photos/_Archive/2010/2010-03/2010-03-01-174249.jpg
Warning: No writable tags set from /mnt/user/Photos/_Archive/2010/2010-03/2010-03-01-174249.jpg
Warning: Invalid tag name '/' - /mnt/user/Photos/_Archive/2010/2010-03/2010-03-01-174249.jpg
    0 image files updated
    1 image files unchanged

Luuk2005

Greetings RadOD. Both of your commands conducted properly inside the Windows cmd.exe, so Im thinking maybe to just change the quotes????
Im no ideas how they should change, but if still having troubles, this can be another way with less quoting...

exiftool -d '%Y%m/%%F' -TestName'<${DateTimeOriginal;s|(..)(.)(.)(..)|$1${2}0/$1$2$3/$2$3-$4|}' file.jpg

Im just guessing with using the single-quotes, but maybe you can experiment until it starts conducting properly.
Windows8.1-64bit,  exiftool-v12.92(standalone),  sed-v4.0.7

RadOD

Ahh, the quotes!  Thanks - I'm in Linux not Windows.  I was looking for "\" vs "/" problems because of the invalid tag name '/' error! 

StarGeek

Yes, on Linux/Mac, you need to use single quotes to enclose a parameter that has a $ to avoid the shell from interpreting it as a variable name.  Double quotes would be used internally to the single quotes, so the DateFmt part would be DateFmt("%Y")
"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