Help with ExifTool Folder Structure Based on Date and Country

Started by Peterjjj, May 10, 2025, 09:13:57 AM

Previous topic - Next topic

Peterjjj

Dear forum members,

My apologies for bothering you, with probabely a very basic question but I am in need of some assistance with an ExifTool script that I have been working on.

I am trying to automatically sort my image and video files based on their creation date and country. The goal is to move the files into directories on a USB Stick organized by year and country. Here's the approach I am attempting:

Create a folder structure where files are first sorted by year (using CreateDate) and then by country (using Country).

Process all file types (JPEG, PNG, MP4, etc.) and move them to a destination directory, preserving the structure based on the EXIF metadata.

Handle cases where metadata such as the creation date or country is missing, ensuring the script continues processing other files.
Like this;

D:\media\2023\Japan\
D:\media\2023\Netherlands\
D:\media\Onbekend\Unkown\
D:\media\2024\Netherlands\

All the commands that I have tried failed (lots to learn here). I have created a .bat file with this code:

@echo off
setlocal

REM Pad to exiftool.exe
set "EXIFTOOL=C:\Users\jeram\Desktop\Fotoverplaatsen\exiftool.exe"

REM Source with subfolders
set "SOURCE=C:\Users\jeram\Dropbox\Afbeeldingen"

REM target on USB-stick
set "TARGET=D:\media"

echo processing...

REM ExifTool-opdracht met voortgang, filtering en sortering
"%EXIFTOOL%" -r -progress -if "$filesize# > 10000" ^
  -Directory="%TARGET%\${CreateDate;DateFmt('%Y')}\${Country}" ^
  -ext jpg -ext jpeg -ext png -ext heic -ext gif -ext mp4 -ext mov -ext avi -ext aac -ext wav ^
  "%SOURCE%"

echo ready!
pause

I am encountering the following issues:

Tags not found: The script runs into errors when certain tags (such as Country) are missing, and I receive warnings like "Tag 'Country' not defined."

Folder naming issue: The folders are not being created as expected. Instead of dynamically populating the directory path with the actual year and country, the script uses the literal strings like ${CreateDate;DateFmt('%Y')}.

Metadata missing: Some files do not contain the necessary metadata (like CreateDate or Country), and this leads to issues where the file is not being processed properly.

Could you kindly provide any advice on how to structure the command to handle missing tags gracefully and ensure the folder creation works correctly?

I apologize for the inconvenience and thank you in advance for your help or link or hint.

Best regards,
Peter

Phil Harvey

Hi Peter,

Sorry for the delay in responding.  This is my busy time of year.

First, let's be sure the is is a BAT file to be used for CMD.EXE because I can't help with PowerShell.

Next, the command should look like this in the BAT file.  Note that all "%" characters in the ExifTool command must be doubled so they aren't interpreted as BAT variable names:

%EXIFTOOL%" -r -progress -if "$filesize# > 10000" ^
  -Directory="%TARGET%/NoDate/NoCountry" ^
  -Directory="%TARGET%/NoDate/${Country}" ^
  -Directory="%TARGET%/${CreateDate;DateFmt('%%Y')}/NoCountry" ^
  -Directory="%TARGET%/${CreateDate;DateFmt('%%Y')}/${Country}" ^
  -ext jpg -ext jpeg -ext png -ext heic -ext gif -ext mp4 -ext mov -ext avi -ext aac -ext wav ^
  "%SOURCE%"


Note that I have used forward slashes instead of backslashes because backslashes are sometimes used for escaping, and forward slashes are just fine with ExifTool file names.  I'm assuming the carat is used for line continuation, but I'm not a BAT file expert so I'll defer to you for that one.

The multiple assignments are used to handle the cases of missing tags, and the last valid assignment will take precedence.

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