I have an huge amount of file to organize, they are a JPG files in an absurd folders structure.
I need to move all files from its actual position to an organized folder structure, saving its actually position as EXIF comment because in path there information to preserve.
For example I have a file with this path:
/OLD ARCHIVE/ORGANIZATION 1/1 FANTASTIC EVENT/1995 PIPPO does something.jpg
The destination will be something like: Archive Digital/AA/1995/CCC_BB_000002.jpg
My idea is to run the script from the OLD ARCHIVE/ path in order to have all information in the fname var. Then extract information from fname var and put them in exif (and in path), at the end, move the file logging what I’m doing.
I am writing this in bash, it was working but had some problem with whitespace management, so I tried to make it whitespace compliant…but it is not working anymore.
Can someone help me?
Thanks in advance.
#!/bin/bash
#Parametri: CartellaOriginale CartellaOutput Prefisso NumeroIniziale Keywords
filenamePrefix="$3"
filenameNumber=$(printf "%05d" $4)
data=$(date +%F)
filelog="fileMovedLog_$data.txt"
CommonKeywords="$5"
echo "DEV Working on $1"
echo "DEV output folder $2"
echo "DEV filename start $filenamePrefix$filenameNumber"
echo "$1/$filelog"
echo "Backup in ${1%/}_BAK/"
#echo "$data"
read -p "Press enter to continue"
echo 'Backup in corso'
#cp -R "$1" "${1%/}_BAK/"
echo 'Backup terminato, inizio elaborazione'
#mkdir "$2"
touch "$1/$filelog"
find "$1" -name "*.jpg" -or -name "*.JPG" -type f -not -path "*_BAK"| while read fname; do
echo "$fname"
#Chiedo l'anno
forseAnno=$(echo `grep -oP '(?<!d)d{4}(?!d)' <<< "$fname"`)
if ( -z "$forseAnno" )
then
forseAnno='ND'
fi
echo "Inserire cartella anno dove inserire la foto oppure lasciare vuoto se $forseAnno"
read annoIn </dev/tty
if ( -z "$annoIn" )
then #Se input è vuoto
anno="$forseAnno"
else #ho input, metto lì.
anno="$annoIn"
fi
#echo "$anno"
#Keyword per Lightroom
#Estraggo alcune possibili KeyWord
string=${fname,,}
extraKey=''
if grep -q 'palio' <<< "$string"; then
extraKey=',palio del niballo'
fi
if grep -q 'dama' <<< "$string"; then
extraKey="${extraKey},dama"
fi
if grep -q 'not' <<< "$string"; then
extraKey="${extraKey},nott de biso"
fi
if grep -q 'cavaliere' <<< "$string"; then
extraKey="${extraKey},cavaliere"
fi
if grep -q 'bigorda' <<< "$string"; then
extraKey="${extraKey},bigorda"
fi
if grep -q 'corteo' <<< "$string"; then
extraKey="${extraKey},corteo"
fi
if grep -q 'paggi' <<< "$string"; then
extraKey="${extraKey},paggi"
fi
echo "$extraKey"
read -e -p "Correggere le Keywords: " -i "$anno,$CommonKeywords$extraKey" KEYWORD </dev/tty
suffix=$(printf "%05d" $filenameNumber)
exiftool -p -Keywords='"$KEYWORD $fname"' -overwrite_original
#Registro il precedente nome file nella descrizione
exiftool -p -Description+=''ERA: "$fname"'' "$fname" -overwrite_original
#Sistemo la CreateDate
echo `exiftool -wm cg -CreateDate='"$anno":01:01 00:00:00' -overwrite_original "$fname"`
if ( $? -ne 0 ) #avevo già una data
then
datafile=(exiftool -S -createdate "$fname")
while true
do
if ( $anno -lt 2021 ) #la scrittura della data in EXIF la faccio solo se è un anno minore di 2021. Se ho messo 5060 per indicare anni 50-60 non lo metto
then
read -e -p "Sovrascrivere la data $datafile con $anno?(Yes/No/Vuoto)" -i "Y" yn </dev/tty
else
read -e -p "Sovrascrivere la data $datafile con $anno?(Yes/No/Vuoto)" -i "N" yn </dev/tty
fi
case $yn in
(Yy)* ) exiftool -p -CreateDate='"$anno":01:01 00:00:00' -overwrite_original;break;;
(Nn)* ) break;; #Non fo una sega
(0123456789)(0123456789)(0123456789)(0123456789)* ) exiftool -p -CreateDate='"$yn":01:01 00:00:00' -overwrite_original;break;;
* ) echo "Inserire Y(es)/N(o) o l'anno";;
esac
done
fi
#Procedo allo spostamento e logging
echo 'Sposto '"$fname $2/$anno/$filenamePrefix$suffix.jpg"
read -p "Confermi?"
mv ""$fname"" ""$2/$anno/$filenamePrefix$suffix.jpg""
echo ""$fname"; "$filenamePrefix$suffix.jpg"" >>$1/$filelog
((filenameNumber++))
echo ""
done
echo "Completato";
```