#!/bin/sh #todo: bei jeder Aenderung auch usage (doc) anpassen #todo: translate entire script into english # adds date taken in beginning of file name if jpg, .png, (not yet: .mp4, .mov, ...) # Attention: there is a hard link to this script at the end!!! moving this script meens changing it # options # -h --help display help # -a --noask --no-interactive do not prompt the user to confirm renaming # -l --onlylog just logs what would have been done but not doing anything # -o [file] --outputfile [file] specifies the output (log) file (standard is da.log) # -v [file] --voutputfile [file] specifies the verbose output log file (standard dav.log) # to be implemented in other sript: # -i --image adds date taken in beginning of file name if jpg, .png, (not yet: .mp4, .mov, ...) #Functions vLog() { echo "$1" >> "$VLOGFILE" } log() { echo "$1" >> "$NVLOGFILE" echo "$1" >> "$VLOGFILE" } rename() # moves $1 to $2 if rn is 1, otherwise set variable new to $1 # log move on any case { log "rn \"$1\" to \"$2\"" if [ $rn -eq 1 ] then mv -- "$1" "$2" else new="$1" fi } remove() # deletes $1 if rn is 1, otherwise only logging { if test -f "$1" then #echo "$1" log "rm \"$1\"" if test $rn -eq 1 then trash "$1" fi fi } usage() # displays help message and exit { echo "" echo "help page for $0" echo "---------------------------------" echo "this script should not be called directly but by the Wrapper script WrapperAddDateTaken.sh" echo "Usage: bash $0 [OPTIONS] DIR" echo " $0 adds the date the picture was taken or movie was filmed at the beginning of the file name." echo "" echo "options" echo " -h, --help display this help" echo " -a, --noask --no-interactive do not prompt the user to confirm renaming (standard is asking for each file)" echo " -l, --onlylog just logs what would have been done but not doing anything" echo " -o [file], --outputfile [file] specifies the output (log) file (standard is da.log)" echo " -v [file], --voutputfile [file] specifies the verbose output log file (standard dav.log)" echo " -- finishes the list of options, next argument is taken as the dir" #echo " -i, --image adds date taken in beginning of file name if jpg, .png, (not yet: .mp4, .mov, ...)" echo "" echo "error handling" echo " if DIR is no valid directory, the scripts eith error code 1" exit } folderloc() # returns the argument as absolute path # arg1: if arg1 starts with /, return arg1, otherwise return $(pwd)/arg1 { case "$1" in /* ) echo "$1" break ;; * ) echo "$(pwd)/$1" break ;; esac } # considering options # saving all options to give them recursively ops="" # standard behavior: ask the user is file is to be renamed # 0 meens not asking ask=1 # standard behavior: actually rename # 0 meens not renaming but just log what would have be done rn=1 # standard behaviour: rename as usual # 1 meens adding date taken to image and video files # image=0 # the log file for verbose logging #VLOGFILE="$1" VLOGFILE="$(pwd)"/../rnv.log # the log file for short logging #NVLOGFILE="$2" NVLOGFILE="$(pwd)"/../rn.log #directory to be checked dir="$(pwd)" # standard if not specified # -n = string is non-zero length? while test -n "$1" ; do case $1 in -h | --help ) usage;; -o | --outputfile ) # ops="$ops -o ../$2" shift NVLOGFILE="$(folderloc "$1")" ;; -v | --voutputfile ) #ops="$ops -v ../$2" shift VLOGFILE="$(folderloc "$1")" ;; -a | --noask | --no-interactive ) ops="$ops -a" ask=0 ;; -l | --onlylog ) ops="$ops -l" rn=0 ;; -- ) shift dir="$(folderloc "$1")" break ;; * ) dir="$(folderloc "$1")" break ;; esac shift # must be done in any case: the next argument is $1 done # echo "options" # echo "asking=$ask" # echo "rename=$rn" # echo "log file=$NVLOGFILE" # echo "verbose log file=$VLOGFILE" # echo "direc=$dir" #for testing purposes: # exit addDates() # adds the picture taken dates to all pictures and descends into subfolders { for fil in * do if test -d "$fil" then cd "$fil" addDates cd .. vLog "went back to $(pwd)" continue #vLog "handle dir" # * als einziger Dateiname kommt auch bei leerem Ordner vor fi if test -e "$fil" # file to be moved exists then : # ok, do what comes after fi else continue fi case "$fil" in 201[0-9]_[01][0-9]_[0-3][0-9]*) vLog "dont rename bc already date there: $fil";; *.[jJ][pP][gG]|*.[jJ][pP][eE][gG]|*.[pP][nN][gG]) #|*.3gp|*.[aA][vV][iI]) date=$(exiftool -d "%Y_%m_%d_%H%M%S" -CreateDate "$fil" | awk '{print $4}') # -z length is zero if test -z "$date" then vLog "the file has no creation date: $fil" else # remove SAM_Zahl new="$(echo "$fil" | sed 's/SAM_[0-9]\{4\}//')" # remove DSC_Zahl (e.g. Hollys camera) new="$(echo "$new" | sed 's/DSC_[0-9]\{4\}//')" # remove DSCZaahl new="$(echo "$new" | sed 's/DSC[0-9]\{5\}//')" # remove DSCFZahl new="$(echo "$new" | sed 's/DSCF[0-9]\{4\}//')" # remove DSCIZahl new="$(echo "$new" | sed 's/DSCI[0-9]\{4\}//')" # remove DSCNZahl new="$(echo "$new" | sed 's/DSCN[0-9]\{4\}//')" # remove IMGPZahl (e.g. Romy Fischers camera) new="$(echo "$new" | sed 's/IMGP[0-9]\{4\}//')" #remove Zah-Zahl_IMG new="$(echo "$new" | sed 's/[0-9]\{3\}-[0-9]\{4\}_IMG//')" # remove IMG_Daaatuum_Zeeiit (e.g. my smartphone) new="$(echo "$new" | sed 's/IMG_20[0-9]\{6\}_[0-9]\{6\}//')" # remove IMG_Zahl new="$(echo "$new" | sed 's/IMG_[0-9]\{4\}//')" # remove PZaaaahl new="$(echo "$new" | sed 's/P[0-9]\{7\}//')" # remove PCZaaahl new="$(echo "$new" | sed 's/P[ABC][0-9]\{6\}//')" # remove PICTZahl new="$(echo "$new" | sed 's/PICT[0-9]\{4\}//')" # remove SDCZaahl new="$(echo "$new" | sed 's/SDC[0-9]\{5\}//')" # remove RIMGZahl new="$(echo "$new" | sed 's/RIMG[0-9]\{4\}//')" # remove Zaaaaaaaaahl_0Mbelieb--.Z (my smart phone at second bluetooth copy) new="$(echo "$new" | sed 's/[0-9]\{14\}_0M.\{6\}\--\.[1-9]//')" # remove 0beliebi--.Z (my smart phone) new="$(echo "$new" | sed 's/0.\{7\}--\.[1-9]//')" # remove 0CLxbelb--.Z (my smart phone) new="$(echo "$new" | sed 's/0CLx.\{6\}--\.[1-9]//')" # remove FotoZahl new="$(echo "$new" | sed 's/Foto[0-9]\{4\}//')" # hochzeitsbilder 180505_Za_Hochzeit (Z) new="$(echo "$new" | sed 's/180505_[01][0-9]_Hochzeit ([0-9]*)/Hochzeit/')" case "$new" in _*|.*) ;; #tue nichts *) new="_$new";; esac new="$date$new" # date und _ haben keine Leerzeichen # existiert eine Datei mit dem neuen Namen bereits? # -e ist exist. die Datei schon? # different jpeg extensions: new="$(echo "$new" | sed 's/\.JPG/.jpg/g')" new="$(echo "$new" | sed 's/\.JPEG/.jpg/g')" new="$(echo "$new" | sed 's/\.jpeg/.jpg/g')" if test -e "$new" then log "a file with name \"$new\" already exists, -> dont rename: $fil" else if test $ask -eq 0 then rename "$fil" "$new" else # Abfrage ob man wirklich umbenennen will echo "rename $fil to $new? enter, y, yes = yes" read q if test -z "$q" -o "$q" = "y" -o "$q" = "yes" then rename "$fil" "$new" fi fi fi fi ;; *) vLog "dont rename bc no image: $fil" esac done } #actual script # check if the argument is correct: if test ! -d "$dir" then log "exiting bc there is no such dir as $dir" #echo "exiting bc there is no such dir" exit 1 fi vLog "move to $dir" cd "$dir" addDates # merke: in Bedingungsklammern [] von if muss am Anfang und am Ende ein " " (blank) stehen!! # merke: Hinweise zu Bedingungen in if abrufbar unter "man test" # merke: bei Zuweisungen mit = darf zwischen Variablenname und = kein Leerzweichen sein! # (sonst wird Variablenname als command interpretiert)