move files

This commit is contained in:
Danilo Reyes 2024-07-16 14:45:59 -06:00
parent a8a7904716
commit d420f9ecd3

View File

@ -45,7 +45,7 @@ extract_datetime_from_filename() {
minute=$((10#$minute)) minute=$((10#$minute))
second=$((10#$second)) 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="" datetime=""
fi fi
else else
@ -60,10 +60,25 @@ log_change() {
photo="$1" photo="$1"
old_date="$2" old_date="$2"
new_date="$3" new_date="$3"
echo "File: $photo" >> "$temp_file" action="$4"
echo " Old Date: $old_date" >> "$temp_file" touch "$temp_file"
echo " New Date: $new_date" >> "$temp_file" {
echo "" >> "$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 # Loop through each year directory
@ -85,7 +100,7 @@ for year_dir in "$base_dir"/*/; do
# Extract the date from the photo metadata # Extract the date from the photo metadata
photo_date=$(exiftool -DateTimeOriginal -d "%Y:%m:%d %H:%M:%S" "$photo" | awk -F': ' '{print $2}') 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 # Scenario 2: No date metadata or invalid date
filename_datetime=$(extract_datetime_from_filename "$photo") filename_datetime=$(extract_datetime_from_filename "$photo")
@ -103,7 +118,6 @@ for year_dir in "$base_dir"/*/; do
else else
photo_year=$(echo "$photo_date" | cut -d':' -f1) photo_year=$(echo "$photo_date" | cut -d':' -f1)
photo_month=$(echo "$photo_date" | cut -d':' -f2) photo_month=$(echo "$photo_date" | cut -d':' -f2)
photo_day=$(echo "$photo_date" | cut -d':' -f3)
if [ "$photo_year" != "$year" ] || [ "$photo_month" != "$month" ]; then if [ "$photo_year" != "$year" ] || [ "$photo_month" != "$month" ]; then
# Scenario 1: Date mismatch # Scenario 1: Date mismatch
@ -112,14 +126,13 @@ for year_dir in "$base_dir"/*/; do
if [ -n "$filename_datetime" ]; then if [ -n "$filename_datetime" ]; then
filename_year=$(echo "$filename_datetime" | cut -d':' -f1) filename_year=$(echo "$filename_datetime" | cut -d':' -f1)
filename_month=$(echo "$filename_datetime" | cut -d':' -f2) 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 if [ "$photo_year" == "$filename_year" ] && [ "$photo_month" == "$filename_month" ]; then
echo "Mismatch found: $photo" echo "Mismatch found: $photo"
echo " Directory date: $year/$month" echo " Directory date: $year/$month"
echo " Photo date: $photo_date" echo " Photo date: $photo_date"
echo " Filename date matches photo date, file is in the wrong directory." 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 else
new_date="$filename_datetime" new_date="$filename_datetime"
log_change "$photo" "$photo_date" "$new_date" log_change "$photo" "$photo_date" "$new_date"
@ -144,10 +157,11 @@ for year_dir in "$base_dir"/*/; do
done done
# Ask for confirmation before proceeding to the next year # 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 if [[ "$response" != "y" ]]; then
echo "Stopping script." echo "Stopping script."
rm -f "$temp_file" rm -f "$temp_file"
touch "$temp_file"
exit 0 exit 0
else else
# Apply changes and save log # Apply changes and save log
@ -156,11 +170,19 @@ for year_dir in "$base_dir"/*/; do
photo=$(echo "$line" | cut -d' ' -f2-) photo=$(echo "$line" | cut -d' ' -f2-)
elif [[ "$line" == " New Date:"* ]]; then elif [[ "$line" == " New Date:"* ]]; then
new_date=$(echo "$line" | cut -d' ' -f3-) 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 fi
done < "$temp_file" done <"$temp_file"
cat "$temp_file" >> "$log_file" cat "$temp_file" >>"$log_file"
rm -f "$temp_file" rm -f "$temp_file"
touch "$temp_file"
fi fi
done done