Created
November 14, 2024 09:26
-
-
Save altbdoor/96d06ab1d3606627e695f2e1f39d09d0 to your computer and use it in GitHub Desktop.
AI/GPT generated Bash script to rename and set dates with exiftool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Folder where your images are located | |
folder="./" | |
# Loop through each image in the folder | |
for filename in "$folder"/*; do | |
# Extract the date and time from the filename using regex | |
if [[ $filename =~ ([a-z]{3,})-([0-9]{1,2})[a-z]{2}-([0-9]{4})-at-([0-9]{1,2})([0-9]{2})-([ap]m)\.[a-z]{3,4} ]]; then | |
month="${BASH_REMATCH[1]}" | |
day="${BASH_REMATCH[2]}" | |
year="${BASH_REMATCH[3]}" | |
hour="${BASH_REMATCH[4]}" | |
minute="${BASH_REMATCH[5]}" | |
ampm="${BASH_REMATCH[6]}" | |
# Convert month name to month number | |
case "${month,,}" in | |
january) month_num="01" ;; | |
february) month_num="02" ;; | |
march) month_num="03" ;; | |
april) month_num="04" ;; | |
may) month_num="05" ;; | |
june) month_num="06" ;; | |
july) month_num="07" ;; | |
august) month_num="08" ;; | |
september) month_num="09" ;; | |
october) month_num="10" ;; | |
november) month_num="11" ;; | |
december) month_num="12" ;; | |
esac | |
# Adjust hour for 12-hour format (PM/AM) | |
if [[ "$ampm" == "pm" && "$hour" != "12" ]]; then | |
hour=$((hour + 12)) # Convert PM to 24-hour format | |
elif [[ "$ampm" == "am" && "$hour" == "12" ]]; then | |
hour="00" # Midnight is 00 in 24-hour format | |
fi | |
# Ensure hour is in two-digit format (e.g., "01" instead of "1") | |
day=$(printf "%02d" "${day#0}") | |
hour=$(printf "%02d" "${hour#0}") | |
minute=$(printf "%02d" "${minute#0}") | |
# Format the extracted data to match EXIF date format | |
exif_date="${year}:${month_num}:${day} ${hour}:${minute}:00" | |
extension="${filename##*.}" | |
exif_filename="${year}-${month_num}-${day}_${hour}-${minute}.$extension" | |
cp "$filename" "./fixxed/$exif_filename" | |
# Set the DateTimeOriginal tag with exiftool | |
exiftool -overwrite_original -CreateDate="$exif_date" -ModifyDate="$exif_date" -DateTimeOriginal="$exif_date" "./fixxed/$exif_filename" | |
echo "Updated EXIF date for $filename to $exif_date" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment