When running an exiftool command on images I sometimes encounter errors concerning the image(s) having some defect that prevents exiftool from reading them as jpegs or pngs, such as "error: not a valid jpeg."
What's a command I could incorporate into the following bash script that will move any images with errors to a subfolder of the working directory?
#!/bin/bash
echo "Please type and press enter"
read conch
if [ -z "$conch" ]; then
printf "no caption was selected, aborting!\n"
exit 1
fi
printf "conch is $conch\n"
exiftool -r -o . -if '$title=~/'$conch'/i' -directory=title .
fi
shift
done
I can't think of an easy way to do this other than capturing the output and searching it for Error messages, then parsing out the file names. This would be a bit of fancy scripting though.
- Phil
Edit: I may be able to add a feature to allow this to be done more easily. Let me think about this.
ExifTool 11.97 has an experimental -efile option that will write the names of the files that give errors. With this version, the command would look like this:
exiftool -r -o . -if '$title=~/'$conch'/i' -directory=title -efile out.txt .
And after the command is run, out.txt will contain the names of any files that gave errors.
Currently, the file names are appended to the efile file, so you should be sure it doesn't exist before this command.
I'm debating whether or not this is a good way to do it. Your feedback would be helpful.
- Phil
Thank you! I just tested it on a batch of jpegs which I knew had some files which would cause errors. And it worked, successfully outputting the names of the problem files to the text file.
I should provide the correction that the script I mistakenly posted in the OP is not the script I intend to use this new option with, but the following script:
#!/bin/bash
echo "Please type and press enter"
read conch
if [ -z "$conch" ]; then
printf "no caption was selected, aborting!\n"
exit 1
fi
printf "conch is $conch\n"
exiftool -all= -title=$conch -efile out.txt -overwrite_original *.jpg *.png
fi
shift
done
I used the following code to take the out.txt and move the problem files to a subfolder.
mkdir ./errors && for file in $(cat out.txt); do mv "$file" ./errors; done
Ideally I'd like to incorporate the above code into the bash script.
Try this:
#!/bin/bash
echo "Please type and press enter"
read conch
if [ -z "$conch" ]; then
printf "no caption was selected, aborting!\n"
exit 1
fi
printf "conch is $conch\n"
rm -f out.txt
exiftool -all= -title="$conch" -efile out.txt -overwrite_original -ext jpg -ext png . -execute -directory=error -@ out.txt
rm -f out.txt
fi
shift
done
... but I would suggest specifying a scratch directory instead of just "out.txt". Something like "/scratch/out.txt".
Note that the -efile option won't currently create directories if necessary. (but maybe it should)
- Phil
Edit: Put quotes around "$conch" on command line. Also changed to use -ext option instead of wildcards (see common mistake 2c,d,e (https://exiftool.org/mistakes.html#M2)).