Use exiftool to declare a variable in while loop

Started by BoudewijnD, February 06, 2017, 03:33:29 PM

Previous topic - Next topic

BoudewijnD

Hi all,

After using a simple but smart bash script en Exiftool (ofcourse ;)) to sort my pictures I was looking for ways to improve it.
In de script I would like to declare a varible with the model name of the file and use that to sort the file in the correct folder. The file doesnot have to be renamed (too long file names...)

Can you guys help out? This is what I have so var:
The varible i would like to use is ModelName
#!/bin/bash
#
#
PhotosPath="/mnt/Data/Foto's/TEMP"
SortPath="/mnt/Data/Foto's/.imagesort"
LibraryPath="/mnt/Data/Foto's/test1234"
CameraPath="gphoto2://[usb:002,007]/DCIM/101CANON"
CharFromName=4
CharFromNameMonth=2

echo
echo
############
# Test to see if $PhotosPath exists, if not promp for new path / exit.
test -d $PhotosPath || read -p "$PhotosPath does not exist, close to exit or type new path:" PhotosPath
test -d $PhotosPath || "read -p '$PhotosPath is invalid. Press enter to close' && exit"

############
echo "Photo's will be copied from $CameraPath to sorting folder"
# move files for camera to $PhotosPath
mv -v $CameraPath/* $PhotosPath/
echo "Photo's will be copied from $PhotosPath to sorting folder"
# move files from camera to $SortPath
mv -v $PhotosPath/* $SortPath/

############
# rename all image files in $SortPath
# FolderDateDD-HHMMSS.ext
echo "Photo's will be renamed like: YYYYMMDD-OriginalFileName.ext"
exiftool -progress '-filename<CreateDate' -d %Y%m%d-%%f.%%le -r -m $SortPath/*
#jhead  -autorot -ft -nf%Y%m%d-%H%M%S $SortPath/*

###########
# Sort files into folders using $CharFromName letters of the file name
#
ls $SortPath | while read file; do
# extract first $CharFromName characters from filename
ModelName = test exiftool -f -'Model'
echo $ModelName
FolderYear=${file:0:$CharFromName}
if [ ${file:4:$CharFromNameMonth} -eq 01 ]
then
FolderMonth="Januari"
elif [ ${file:4:$CharFromNameMonth} -eq 02 ]
then
FolderMonth="Februari"
elif [ ${file:4:$CharFromNameMonth} -eq 03 ]
then
FolderMonth="Maart"
elif [ ${file:4:$CharFromNameMonth} -eq 04 ]
then
FolderMonth="April"
elif [ ${file:4:$CharFromNameMonth} -eq 05 ]
then
FolderMonth="Mei"
elif [ ${file:4:$CharFromNameMonth} -eq 06 ]
then
FolderMonth="Juni"
elif [ ${file:4:$CharFromNameMonth} -eq 07 ]
then
FolderMonth="Juli"
elif [ ${file:4:$CharFromNameMonth} -eq 08 ]
then
FolderMonth="Augustus"
elif [ ${file:4:$CharFromNameMonth} -eq 09 ]
then
FolderMonth="September"
elif [ ${file:4:$CharFromNameMonth} -eq 10 ]
then
FolderMonth="Oktober"
elif [ ${file:4:$CharFromNameMonth} -eq 11 ]
then
FolderMonth="November"
elif [ ${file:4:$CharFromNameMonth} -eq 12 ]
then
FolderMonth="December"
fi
# create directory if it does not exist
test -d $LibraryPath/$FolderYear/$FolderYear-$FolderMonth/$ModelName/ || mkdir -p $LibraryPath/$FolderYear/$FolderYear-$FolderMonth/$ModelName/
# move the current file to the destination dir
mv -v $SortPath/$file $LibraryPath/$FolderYear/$FolderYear-$FolderMonth/$ModelName/$file
done

##########
# move sorted files into photo library
#mv -v $SortPath/* $LibraryPath/

##########
# Umount the card
umount $CameraPath

##########
# End notification
echo
echo "Photos  from: $PhotosPath"
echo "End location: $LibraryPath"
echo
echo "The card has been ejected."
echo
read -p "Press enter to close this window..."


Hayo Baan

Hello Boudewijn,

Have you already tried the back tick operator? E.g. like so: ModelName=`exiftool -q -ModelName FILE` this wil capture the output of the exiftool command in the variable. You can use whatever options or commands you like this way. The backtick is a very useful feature :)
Hayo Baan – Photography
Web: www.hayobaan.nl

StarGeek

I'm not too familiar with bash, but from what I can tell, it looks like this could be made into a single exiftool command to rename and sort it all.  The only hard part would be converting the English month names to German but that still could be done in-line.   

Here's what I came up with, it would still need some tweaks as I don't know how to handle the $LibraryPath variable:
exiftool -d /%Y/%Y-%M '-filename<${CreateDate;my @gMon=qw(ignore Januari Februari Maart April Mei Juni Juli Augustus September Oktober November December);s/(\d\d)$/$gMon[$1]/}/$Model/${createdate#;s/(\d{4}):(\d\d):(\d\d).*/$1$2$3/}-$filename' FileOrDir

Example (using TestName)
C:\>exiftool -d /%Y/%Y-%M "-testname<${CreateDate;my @gMon=qw(ignore Januari Februari Maart April Mei Juni Juli Augustus September Oktober November December);s/(\d\d)$/$gMon[$1]/}/$Model/${createdate#;s/(\d{4}):(\d\d):(\d\d).*/$1$2$3/}-$filename" X:\!temp\Test3.jpg
'X:/!temp/Test3.jpg' --> '/2002/2002-Februari/EXIF:Model/20020202-Test3.jpg'
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

BoudewijnD

Hi Guys,

Thanks to some pointers from Hayo I was able to fix my problem. My script will now auto update the file names and sort them in year by month and if it is taken by my "Powershot"  it will put it in a new folder. all the DSLR files will remain in there old location. The whole code is found below. There are bit of old code still in there to copy directly from the DSLR, however this is not up and running yet!

#!/bin/bash
#
#
PhotosPath="/mnt/Data/Foto's/TEMP"
SortPath="/mnt/Data/Foto's/.imagesort"
LibraryPath="/mnt/Data/Foto's"
#CameraPath="gphoto2://[usb:002,007]/DCIM/101CANON"
CharFromName=4
CharFromNameMonth=2
echo
echo
############
# Test to see if $PhotosPath exists, if not promp for new path / exit.
#test -d $PhotosPath || read -p "$PhotosPath does not exist, close to exit or type new path:" PhotosPath
#test -d $PhotosPath || "read -p '$PhotosPath is invalid. Press enter to close' && exit"

############
#echo "Photo's will be copied from $CameraPath to sorting folder"
# move files for camera to $PhotosPath
#mv -v $CameraPath/* $PhotosPath/
#echo "Photo's will be copied from $PhotosPath to sorting folder"
# move files from camera to $SortPath
mv -v $PhotosPath/* $SortPath/

############
# rename all image files in $SortPath
# FolderDateDD-HHMMSS.ext
echo "Photo's will be renamed like: YYYYMMDD-OriginalFileName.*"
exiftool -progress '-filename<CreateDate' -d %Y%m%d-%%f.%%le -r -m $SortPath/*
#jhead  -autorot -ft -nf%Y%m%d-%H%M%S $SortPath/*

###########
# Sort files into folders using $CharFromName letters of the file name
#
ls $SortPath | while read file; do
# find model name -s -s -s is for short name without tagname
ModelName=$(exiftool -s -s -s -'Model' $SortPath/$file)
  # extract first $CharFromName characters from filename
FolderYear=${file:0:$CharFromName}
if [ ${file:4:$CharFromNameMonth} -eq 01 ]
then
FolderMonth="Januari"
elif [ ${file:4:$CharFromNameMonth} -eq 02 ]
then
FolderMonth="Februari"
elif [ ${file:4:$CharFromNameMonth} -eq 03 ]
then
FolderMonth="Maart"
elif [ ${file:4:$CharFromNameMonth} -eq 04 ]
then
FolderMonth="April"
elif [ ${file:4:$CharFromNameMonth} -eq 05 ]
then
FolderMonth="Mei"
elif [ ${file:4:$CharFromNameMonth} -eq 06 ]
then
FolderMonth="Juni"
elif [ ${file:4:$CharFromNameMonth} -eq 07 ]
then
FolderMonth="Juli"
elif [ ${file:4:$CharFromNameMonth} -eq 08 ]
then
FolderMonth="Augustus"
elif [ ${file:4:$CharFromNameMonth} -eq 09 ]
then
FolderMonth="September"
elif [ ${file:4:$CharFromNameMonth} -eq 10 ]
then
FolderMonth="Oktober"
elif [ ${file:4:$CharFromNameMonth} -eq 11 ]
then
FolderMonth="November"
elif [ ${file:4:$CharFromNameMonth} -eq 12 ]
then
FolderMonth="December"
fi
#only if file is from "Canon PowerShot SX260 HS" then make additional folder
if [ "$ModelName" = "Canon PowerShot SX260 HS" ]
then
# create directory if it does not exist
test -d $LibraryPath/$FolderYear/$FolderYear-$FolderMonth/"$ModelName"/ || mkdir -p $LibraryPath/$FolderYear/$FolderYear-$FolderMonth/"$ModelName"/
# move the current file to the destination dir
mv -v $SortPath/$file $LibraryPath/$FolderYear/$FolderYear-$FolderMonth/"$ModelName"/$file
elif [ "$ModelName" != "Canon PowerShot SX260 HS" ]
then
# create directory if it does not exist
test -d $LibraryPath/$FolderYear/$FolderYear-$FolderMonth/ || mkdir -p $LibraryPath/$FolderYear/$FolderYear-$FolderMonth/
# move the current file to the destination dir
mv -v $SortPath/$file $LibraryPath/$FolderYear/$FolderYear-$FolderMonth/$file
fi
done

##########
# move sorted files into photo library
#mv -v $SortPath/* $LibraryPath/

##########
# Umount the card
#umount $CameraPath

##########
# End notification
echo
echo "Photos  from: $PhotosPath"
echo "End location: $LibraryPath"
echo
read -p "Press enter to close this window..."

Hayo Baan

Excellent, good to hear it worked out. (I see you used the $("code") variant of the backtick operator, good!)
Hayo Baan – Photography
Web: www.hayobaan.nl