Date from EXIF to Folder Name

Started by shiro, October 02, 2020, 02:42:26 AM

Previous topic - Next topic

shiro

Hello,
I have photos organized in folders and subfolders like this:
e:\Photos\2010\17.3.2010 - Grill Party\*.jpg
e:\Photos\2012\25.11.2012 - new car\*.jpg
e:\Photos\2015\12.5.2015 - Grandma\*.jpg

And I would like to rename all those subdirectories by extracting date of photo from EXIF info and rename only date portion of folder name (only "17.3.2010", and leave everything else intact). So there its needed to extract date from EXIF from only one of files in such directory and rename first 9 characters of its folder name. And do that for every subdirectory under directories of the years (2010, 2011, etc.)

After renaming, is should look like this:
e:\Photos\2010\2010-03-17 - Grill Party\*.jpg
e:\Photos\2012\2012-11-25 - new car\*.jpg
e:\Photos\2015\2015-05-12 - Grandma\*.jpg


...is something like this possible with exiftool?

Thanks.

greybeard

Backup everything first - and test

If you change directory to the Photos subdirectory - then this should work:

exiftool '-directory<${directory;s/\/\d+\.\d+\.\d+.*$/\//}$datetimeoriginal${directory;s/^.*\/\d+\.\d+\.\d+//}' -d "%Y-%m-%d" -r .

The only thing it doesn't do is extract the EXIF date from a single file per subdirectory - it treats every file separately

It creates the directory name in three sections:
- everything before existing date (format \d+.\d+.\d*)
- datetimeoriginal from EXIF reformatted to YYYY-MM-DD (this is the date the image was taken - you don't specify which date you are interested in)
- everything after the existing date

Phil Harvey

You talk about renaming the folders, but your example only changes the file name.

It seems that you just want to change the order of the date fields in the file name from M.D.YYYY to YYYY-MM-DD, and this command should do it:

exiftool "-testname<${filename;s/(\d{1,2})\.(\d{1,2})\.(\d{4})/sprintf('%.4d-%.2d-%.2d',$3,$2,$1)/e}" -r e:\Photos

The above command will show the results of the name changes, but won't rename the files.  Change "testname" to "filename" to actually do the renaming.

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

shiro

I need to change names of subdirectories in which JPEGs are. Not filenames of those JPEGs.  :)

I have directory E:\Photos\, in which are subdirectories for every year (2006 to 2019) and in those subdirectories are more subdirectories about particular events. And in those subdirectories are pictures.
So the entire path to JPEG file is like E:\Photos\2010\17.3.2010 - Grill Party\DSC_001.jpg
But JPEG's filenames are not limited to "DSC_XXYY", they can be anything.

I need exiftool to crawl those subdirectories and read date of when image was taken from EXIF of any (maybe first one?) picture in that particular subdirectory. And use this date to change the date part of the name of subdirectory of that picture to new format. From DD.M.YYYY (or DD.MM.YYYY) to YYYY.MM.DD.

So at the end, it will be like this: E:\Photos\2010\2010-03-17 - Grill Party\DSC_001.jpg


Phil Harvey

Quote from: shiro on October 02, 2020, 08:46:47 AM
I need to change names of subdirectories in which JPEGs are. Not filenames of those JPEGs.  :)

Ah, right.  Now I see the "\" before the "*.jpg".  Sorry.  Ignore my post. :(

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

greybeard

If the date is correct in the existing directories and you just need to reformat it then you can use Phil's technique on the directory name.

This should show you the reformatted names without actually changing anything so that you can verify it will give you what you want:

exiftool '-directory;s/^(.*\/)(\d{1,2})\.(\d{1,2})\.(\d{4})(.*)$/sprintf("%-s%.4d-%.2d-%.2d%-s",$1,$4,$3,$2,$5)/e}' -r e:\Photos

StarGeek

Edit: post typed up before greybeard's posts, so I haven't read those yet

A couple of points to make.  Exiftool can't rename directories.  It will more your files to newly created directories.  The original, hopefully empty originals will still be there.

Since you appear to be on Windows and if you're using CMD, then greybeard's command will need to use double quotes instead of single quotes.  Also, I don't believe it takes into account the parent decade directory.

My own take on the problem would be
exiftool -r -d "%Y/%Y-%m-%d" "-Directory<%-:2d$DateTimeOriginal${Directory;s/^.*\/\d+\.\d+\.\d+//}" e:\Photos\

The %-:2d takes the directory of the file being processed, drops the last two directories.  The -d (dateFormat) option includes the decade directory.
"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

greybeard

"Since you appear to be on Windows and if you're using CMD, then greybeard's command will need to use double quotes instead of single quotes.  Also, I don't believe it takes into account the parent decade directory."

True it will need double quotes using CMD - I ran my original command using Powershell on Windows - but it does take into account the decade subdirectory.

StarGeek

Quote from: greybeard on October 02, 2020, 12:01:16 PMbut it does take into account the decade subdirectory.

Cool, I didn't look closely enough as I was in a rush.
"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

shiro

Hi, thanks for every advice. I tried those commands and here are results:
Quote from: greybeard on October 02, 2020, 04:52:18 AM
If you change directory to the Photos subdirectory - then this should work:
exiftool '-directory<${directory;s/\/\d+\.\d+\.\d+.*$/\//}$datetimeoriginal${directory;s/^.*\/\d+\.\d+\.\d+//}' -d "%Y-%m-%d" -r .
When I run this through CMD from e:\Photos\, it ends with: The system cannot find the file specified.

Quote from: greybeard on October 02, 2020, 11:33:46 AM
If the date is correct in the existing directories and you just need to reformat it then you can use Phil's technique on the directory name.
This should show you the reformatted names without actually changing anything so that you can verify it will give you what you want:

exiftool "-directory;s/^(.*\/)(\d{1,2})\.(\d{1,2})\.(\d{4})(.*)$/sprintf("%-s%.4d-%.2d-%.2d%-s",$1,$4,$3,$2,$5)/e}" -r e:\Photos
The powershell first write: Invalid TAG name: "directory;s/^(.*\/)(\d{1,2})\.(\d{1,2})\.(\d{4})(.*)$/sprintf(%-s%.4d-%.2d-%.2d%-s,$1,$4,$3,$2,$5)/e}", and then just list all files with their paths in decade subdirectory.

When I run this, with double quotes through CMD, either from e:\ or e:\Photos\, it ends with this: Invalid TAG name: "directory;s/^(.*\/)(\d{1,2})\.(\d{1,2})\.(\d{4})(.*)$/sprintf(.4d-.2d\Photos"
No file specified


Quote from: StarGeek on October 02, 2020, 11:44:26 AM
Edit: post typed up before greybeard's posts, so I haven't read those yet
A couple of points to make.  Exiftool can't rename directories.  It will more your files to newly created directories.  The original, hopefully empty originals will still be there.
Since you appear to be on Windows and if you're using CMD, then greybeard's command will need to use double quotes instead of single quotes.  Also, I don't believe it takes into account the parent decade directory.
My own take on the problem would be
exiftool -r -d "%Y/%Y-%m-%d" "-Directory<%-:2d$DateTimeOriginal${Directory;s/^.*\/\d+\.\d+\.\d+//}" e:\Photos\

The %-:2d takes the directory of the file being processed, drops the last two directories.  The -d (dateFormat) option includes the decade directory.
When I run this from e:\Photos\ directory, it makes new directory for every subdirectory of every event, but instead of date, it names them like 2dY-d - Grill Party

The solution may be tougher than it looks :)


greybeard

As StarGeek pointed out - you need to change the quotes if you run my command in Windows through CMD.

After changing directory to the Photos folder - under CMD it should be:

exiftool "-directory<${directory;s/\/\d+\.\d+\.\d+.*$/\//}$datetimeoriginal${directory;s/^.*\/\d+\.\d+\.\d+//}" -d "%Y-%m-%d" -r .


StarGeek

Quote from: shiro on October 03, 2020, 02:02:39 AM
When I run this from e:\Photos\ directory, it makes new directory for every subdirectory of every event, but instead of date, it names them like 2dY-d - Grill Party

Only if you're leaving out a very important piece of information, such as running from a batch file.  See FAQ #27

Example output running the command here
Y:\!temp\aaa>exiftool -G1 -a -s -r -filepath -DateTimeOriginal .
======== ./2010/17.3.2010 - Grill Party/Test1.jpg
[System]        FilePath                        : Y:/!temp/aaa/2010/17.3.2010 - Grill Party/Test1.jpg
[ExifIFD]       DateTimeOriginal                : 2010:03:17 12:00:00
======== ./2012/25.11.2012 - new car/Test1.jpg
[System]        FilePath                        : Y:/!temp/aaa/2012/25.11.2012 - new car/Test1.jpg
[ExifIFD]       DateTimeOriginal                : 2012:11:25 12:00:00
======== ./2015/12.5.2015 - Grandma/Test1.jpg
[System]        FilePath                        : Y:/!temp/aaa/2015/12.5.2015 - Grandma/Test1.jpg
[ExifIFD]       DateTimeOriginal                : 2015:05:12 12:00:00
======== ./aaa.rar
[System]        FilePath                        : Y:/!temp/aaa/aaa.rar
[ExifIFD]       DateTimeOriginal                : 2012:11:25 12:00:00
    7 directories scanned
    4 image files read

Y:\!temp\aaa>exiftool -ext jpg -r -d "%Y/%Y-%m-%d" "-Directory<%-:2d$DateTimeOriginal${Directory;s/^.*\/\d+\.\d+\.\d+//}" .
    7 directories scanned
    3 directories created
    3 image files updated

Y:\!temp\aaa>exiftool -G1 -a -s -r -filepath -DateTimeOriginal .
======== ./2010/2010-03-17 - Grill Party/Test1.jpg
[System]        FilePath                        : Y:/!temp/aaa/2010/2010-03-17 - Grill Party/Test1.jpg
[ExifIFD]       DateTimeOriginal                : 2010:03:17 12:00:00
======== ./2012/2012-11-25 - new car/Test1.jpg
[System]        FilePath                        : Y:/!temp/aaa/2012/2012-11-25 - new car/Test1.jpg
[ExifIFD]       DateTimeOriginal                : 2012:11:25 12:00:00
======== ./2015/2015-05-12 - Grandma/Test1.jpg
[System]        FilePath                        : Y:/!temp/aaa/2015/2015-05-12 - Grandma/Test1.jpg
[ExifIFD]       DateTimeOriginal                : 2015:05:12 12:00:00
======== ./aaa.rar
[System]        FilePath                        : Y:/!temp/aaa/aaa.rar
[ExifIFD]       DateTimeOriginal                : 2012:11:25 12:00:00
   10 directories scanned
    4 image files read
"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

shiro

Ah, there was the problem :) I didn't know that there are differences between running it directly from CMD or BAT file.
From CMD, it working perfectly.

Thank you.