Most Efficient Way to Rename Files and Update CreateDate Tag

Started by davidcpreston, February 20, 2025, 05:51:05 AM

Previous topic - Next topic

davidcpreston

Windows 11: Edited/Created in Windows Photos: Processed using .BAT file: Exiftool 13.19.

All of the files are in subdirectories of C:\Pending. Most file names are date based, but the ones from my cameras are not, but they all have a valid CreateDate. Some of the files that are date based have no CreateDate tag or need to be updated.
What I want to end up with is the most efficient way for all files in the subdirectory to be named based on datetime and folder name with tags updated where appropriate; I don't envisage a duplicate BUT will use the -c option to ensure it can't happen. Can I do it in one pass?

Example 1 "C:\Pending\Hexham Tyne Green\DJI_20250219111050_0005_D.JPG" rename to "2025-02-19 11-10 Hexham Tyne Green.JPG"

Example 2 "C:\Pending\Newcastle Quay\DSCN3725.MP4" with Create Date Tag of 2025:02:18 09:26:42 rename to "2025-02-18 09-26 Newcastle Quay.MP4"

StarGeek

You didn't mention what your condition was for when the CreateDate needs to be updated, but that part alone means it cannot be done in one pass. I'm guessing that you want to compare the numbers in the file name against the value of the CreateDate.
exiftool -if "not $CreateDate or ${Filename;m/(\d{14})/;$_=$1} ne ${CreateDate;DateFmt('%Y%m%d%H%M%S')}" "-CreateDate<Filename" /path/to/files/
But there is a lot of potential for incorrect results. For example, in the case of your MP4 files, are the filenames local time or UTC? And what if they were local time, but in a different time zone than the computer. Both of these situations will overwrite correct time stamps with incorrect ones.

The above command is for Windows CMD and will probably fail on PowerShell even if you swap double/single quotes.

To use the name of parent directory, you would use
${FilePath;$_=(split m(/),$_)[-2]}

The rename command would be
exiftool -d "%Y-%-m-%d %H-%M" "-Filename<$CreateDate ${FilePath;$_=(split m(/),$_)[-2]}.%e" /path/to/files/

The main thing that needs clarification is the time stamps for videos and whether they are correctly saved as UTC or incorrectly saved as local time. Then you also need to take into account videos that were taken in a time zone other than the one the computer is in.
"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

davidcpreston

Great response thanks. 2 passes it is, and you've given me the way to determine if the filename is already date based. I'll check out the effect on the MP4s, though they are of minor concern.
I've already got the file renaming working OK in the subfolders with:-

exiftool.exe -r "-filename<CreateDate" -d "%%Y-%%m-%%d_%%H-%%M-%%S%%%%+c %%%%-1:D.%%%%e" "c:\pending"

2025-02-20_12-47-33 Hexham Sele.JPG
2025-02-20_12-47-33_1 Hexham Sele.JPG

I included seconds and I use some HDR software that produces a number of files with different filters, and these get exactly the same Create Date.
This is the reason I wanted to change the date on the MP4s, as if you save clips from a video they all get exactly the same create date and when they have the sequential number added they are not necessarily in the correct order.
Many thanks for your help, and what a great tool this is


davidcpreston

We have lift off, thanks again for all your help.
Changing the Create Date tag in MP4s caused no problems; I just change the time in the filename when I save the clip, usually only adding a few minutes and/or seconds. Windows doesn't display the create date anywhere for a video and the Media Created date remains unchanged, but I checked it with ExifTool.
This is my batch file:-

@echo off
rem Put all files in C:\Pending - Update CreateDate Tag from filename then rename files based on datetime
rem  so they can easily be moved into sub folders based on subject.
rem Rename files in subfolders based on CreateDate tag and subject from folder name

C:
cd \pending

rem If no files in root folder process sub folders
rem if exist *.* or any variant of is still true if no files except directories exist, so workaround is:-
for /f "delims=" %%a in ('dir /b /a-d') do set filesonly=%%a
rem setlocal enableDelayedExpansion
if "%filesonly%" == "" goto renamefilesinfolders

rem Save files, update CreatDate tag from filename then rename file based on CreateDate Tag
Copy *.* D:\Saved
"D:\Documents\Utilities\exiftool\exiftool.exe" -m -overwrite_original "-CreateDate<filename" *.*
"D:\Documents\Utilities\exiftool\exiftool.exe" "-filename<CreateDate" -d "%%Y-%%m-%%d_%%H-%%M-%%S%%%%+c.%%%%e" *.*
goto end

rem Rename files based on CreateDate tag and folder name (subject)
:renamefilesinfolders
"D:\Documents\Utilities\exiftool\exiftool.exe" -r -fileorder CreateDate "-filename<CreateDate" -d "%%Y-%%m-%%d_%%H-%%M-%%S%%%%+c %%%%-1:D.%%%%e" "c:\pending"

:end