ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: james66 on January 15, 2025, 08:15:44 AM

Title: Sort / Rename Files Sequentially per DateTimeOriginal
Post by: james66 on January 15, 2025, 08:15:44 AM
I hesitate in posting, as this is probably simple and has been posted before, sorry. But I've looked and tried, and have also tied myself in knots in a Windows batch script with various loops and still not nailed it.

I have a set of JPEG photo images named image1-thumb.jpg, image2-thumb.jpg, image3-thumb.jpg etc.

What I want to do is simply sort and rename them so that they still follow that same naming structure, but image1-thumb.jpg will become the name of oldest photo (in terms of the date-time the photo was taken, which I believe is DateTimeOriginal), image2-thumb.jpg will become the name of the second oldest and so on. So they all get renamed in that same format, but in order of date the photo was taken.

Can anyone nudge me in the right direction please? I'm on Windows, using the command line tool. Thank you.
Title: Re: Sort / Rename Files Sequentially per DateTimeOriginal
Post by: Phil Harvey on January 15, 2025, 08:21:05 AM
Hi James,

Assuming that by "oldest", you mean the one with the earliest CreateDate tag, then this command could do it:

exiftool -filename=tmp/image%.ncthumb.jpg -fileorder createdate -ext jpg DIR

The "%.nc" adds a counter that starts at 1.

The trick here is that I had to move the files to a temporary directory ("tmp") to avoid name conflicts during the renaming process.  After this is done, you can move the files back out of the tmp directory.

Files that don't have a CreateDate tag won't be moved/renamed.  If you want to be sure that all files are renamed, you can use something like FileModifyDate (which is guaranteed to exist) instead of CreateDate.

- Phil
Title: Re: Sort / Rename Files Sequentially per DateTimeOriginal
Post by: james66 on January 15, 2025, 09:01:24 AM
Thank you very much for that Phil - I will give it a go. Best wishes.
Title: Re: Sort / Rename Files Sequentially per DateTimeOriginal
Post by: Phil Harvey on January 15, 2025, 09:16:38 AM
One correction.  With that command, files without CreateDate would actually be moved after all the other files.  To avoid moving ones without CreateDate, you could do this:

exiftool -if "$createdate" -filename=tmp/image%.ncthumb.jpg -fileorder createdate -ext jpg DIR

- Phil
Title: Re: Sort / Rename Files Sequentially per DateTimeOriginal
Post by: james66 on January 15, 2025, 09:52:26 AM
Ah, yes Phil, good update. I have this solution now working in my Windows batch script in conjunction with mogrify for resizing / cropping etc as part of my workflow automation. All is good - exiftool is a very powerful and flexible tool. Thank you.