From d420f9ecd33f55f924788101ab2c17f6761d0767 Mon Sep 17 00:00:00 2001 From: Danilo Reyes Date: Tue, 16 Jul 2024 14:45:59 -0600 Subject: [PATCH] move files --- photos-metadata.sh | 50 +++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/photos-metadata.sh b/photos-metadata.sh index 62e86e5..336ee78 100755 --- a/photos-metadata.sh +++ b/photos-metadata.sh @@ -45,7 +45,7 @@ extract_datetime_from_filename() { minute=$((10#$minute)) second=$((10#$second)) - if (( year < 2009 || year > 2024 )) || (( month < 1 || month > 12 )) || (( day < 1 || day > 31 )) || (( hour > 23 )) || (( minute > 59 )) || (( second > 59 )); then + if ((year < 2009 || year > 2024)) || ((month < 1 || month > 12)) || ((day < 1 || day > 31)) || ((hour > 23)) || ((minute > 59)) || ((second > 59)); then datetime="" fi else @@ -60,10 +60,25 @@ log_change() { photo="$1" old_date="$2" new_date="$3" - echo "File: $photo" >> "$temp_file" - echo " Old Date: $old_date" >> "$temp_file" - echo " New Date: $new_date" >> "$temp_file" - echo "" >> "$temp_file" + action="$4" + touch "$temp_file" + { + echo "File: $photo" + echo " Old Date: $old_date" + echo " New Date: $new_date" + if [ -n "$action" ]; then + echo " Suggested action: $action" + fi + echo "" + } >>"$temp_file" +} + +# Function to handle errors and convert images to JPEG using ImageMagick +handle_conversion_error() { + photo="$1" + new_photo="${photo%.*}.jpg" + echo "Converting $photo to $new_photo using ImageMagick." + convert "$photo" "$new_photo" && rm "$photo" } # Loop through each year directory @@ -85,7 +100,7 @@ for year_dir in "$base_dir"/*/; do # Extract the date from the photo metadata photo_date=$(exiftool -DateTimeOriginal -d "%Y:%m:%d %H:%M:%S" "$photo" | awk -F': ' '{print $2}') - if [ -z "$photo_date" ] || [[ "$photo_date" == "0000:00:00 00:00:00" ]] || [[ "$photo_date" == "1970:01:01 00:00:00" ]] || [[ "$photo_date" == "1979:12:31 21:00:00" ]]; then + if [ -z "$photo_date" ]; then # Scenario 2: No date metadata or invalid date filename_datetime=$(extract_datetime_from_filename "$photo") @@ -103,7 +118,6 @@ for year_dir in "$base_dir"/*/; do else photo_year=$(echo "$photo_date" | cut -d':' -f1) photo_month=$(echo "$photo_date" | cut -d':' -f2) - photo_day=$(echo "$photo_date" | cut -d':' -f3) if [ "$photo_year" != "$year" ] || [ "$photo_month" != "$month" ]; then # Scenario 1: Date mismatch @@ -112,14 +126,13 @@ for year_dir in "$base_dir"/*/; do if [ -n "$filename_datetime" ]; then filename_year=$(echo "$filename_datetime" | cut -d':' -f1) filename_month=$(echo "$filename_datetime" | cut -d':' -f2) - filename_day=$(echo "$filename_datetime" | cut -d':' -f3) - if [ "$photo_year" == "$filename_year" ] && [ "$photo_month" == "$filename_month" ]; then echo "Mismatch found: $photo" echo " Directory date: $year/$month" echo " Photo date: $photo_date" echo " Filename date matches photo date, file is in the wrong directory." - echo " Suggested action: Move to $filename_year/$filename_month/$filename_day" + action="Move to $filename_year/$filename_month" + log_change "$photo" "$photo_date" "$filename_datetime" "$action" else new_date="$filename_datetime" log_change "$photo" "$photo_date" "$new_date" @@ -144,10 +157,11 @@ for year_dir in "$base_dir"/*/; do done # Ask for confirmation before proceeding to the next year - read -p "Finished processing year $year. Do you want to save the changes and continue to the next year? (y/n): " response + read -rp "Finished processing year $year. Do you want to save the changes and continue to the next year? (y/n): " response if [[ "$response" != "y" ]]; then echo "Stopping script." rm -f "$temp_file" + touch "$temp_file" exit 0 else # Apply changes and save log @@ -156,11 +170,19 @@ for year_dir in "$base_dir"/*/; do photo=$(echo "$line" | cut -d' ' -f2-) elif [[ "$line" == " New Date:"* ]]; then new_date=$(echo "$line" | cut -d' ' -f3-) - exiftool -overwrite_original -DateTimeOriginal="$new_date" "$photo" + if ! exiftool -overwrite_original -DateTimeOriginal="$new_date" "$photo"; then + handle_conversion_error "$photo" + fi + elif [[ "$line" == " Suggested action: Move to"* ]]; then + target_directory=$(echo "$line" | cut -d' ' -f4-) + target_path="$base_dir/$target_directory" + echo "$target_path" + # mkdir -p "$target_path" && mv "$photo" "$target_path/" fi - done < "$temp_file" + done <"$temp_file" - cat "$temp_file" >> "$log_file" + cat "$temp_file" >>"$log_file" rm -f "$temp_file" + touch "$temp_file" fi done