Created
December 26, 2019 21:55
-
-
Save tjluoma/205e3d85e46eb6025b87c6db5b77375b to your computer and use it in GitHub Desktop.
rename PDF files by their creation date (as determined by PDF metadata)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh -f | |
# Purpose: rename PDFs by their creation date | |
# | |
# From: Timothy J. Luoma | |
# Mail: luomat at gmail dot com | |
# Date: 2019-12-26 | |
NAME="$0:t:r" | |
if [[ -e "$HOME/.path" ]] | |
then | |
source "$HOME/.path" | |
else | |
PATH="$HOME/scripts:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin" | |
fi | |
if ((! $+commands[pdfinfo] )) | |
then | |
# note: if pdfinfo is a function or alias, it will come back not found | |
echo "$NAME: 'pdfinfo' is required but not found in $PATH" >>/dev/stderr | |
echo "if you use brew, you can install it via 'brew install poppler'" >>/dev/stderr | |
exit 1 | |
fi | |
for i in "$@" | |
do | |
[[ ! -e "$i" ]] && echo "$NAME: '$i' does not exist." >>/dev/stderr && continue | |
[ "$i:e:l" != "pdf" ] && echo "$NAME: '$i' is not a PDF." >>/dev/stderr && continue | |
# make variable full path | |
i=($i(:A)) | |
# note there are 3 spaces after -F' and before the second ' | |
CDATE=($(pdfinfo "$i" | awk -F' ' '/^CreationDate:/{print $NF}')) | |
[[ "$CDATE" == "" ]] && echo "$NAME: Unable to get CreationDate of '$i'." >>/dev/stderr && continue | |
MONTH="$CDATE[2]" | |
DAY="$CDATE[3]" | |
YEAR="$CDATE[5]" | |
# convert 1-digit day to 2-digit day | |
case "$DAY" in | |
1) DAY='01' ;; | |
2) DAY='02' ;; | |
3) DAY='03' ;; | |
4) DAY='04' ;; | |
5) DAY='05' ;; | |
6) DAY='06' ;; | |
7) DAY='07' ;; | |
8) DAY='08' ;; | |
9) DAY='09' ;; | |
esac | |
# convert month 3-letter-abbreviation to 2-digit month | |
case "$MONTH" in | |
Jan) MONTH='01' ;; | |
Feb) MONTH='02' ;; | |
Mar) MONTH='03' ;; | |
Apr) MONTH='04' ;; | |
May) MONTH='05' ;; | |
Jun) MONTH='06' ;; | |
Jul) MONTH='07' ;; | |
Aug) MONTH='08' ;; | |
Sep) MONTH='09' ;; | |
Oct) MONTH='10' ;; | |
Nov) MONTH='11' ;; | |
Dec) MONTH='12' ;; | |
esac | |
# echo "'$i' was created on '${YEAR}-${MONTH}-${DAY}'." | |
CDATE_FORMATTED="${YEAR}-${MONTH}-${DAY}" | |
TEST_NAME=$(echo "$i" | sed "s#${CDATE_FORMATTED}##g") | |
if [[ "$TEST_NAME" == "$i" ]] | |
then | |
# echo "Does not have date in it" | |
SHORT="$i:t:r" | |
DIR="$i:h" | |
NEWNAME="$DIR/$SHORT ($CDATE_FORMATTED).pdf" | |
COUNT='0' | |
# if a file with that name already exists, add number to end of filename | |
while [[ -e "$NEWNAME" ]] | |
do | |
((COUNT++)) | |
NEWNAME="$DIR/$SHORT ($CDATE_FORMATTED) $COUNT.pdf" | |
done | |
mv -vn "$i" "$NEWNAME" | |
else | |
echo "$NAME: '$i' already has the date in the filename." | |
fi | |
done | |
exit 0 | |
#EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Written to help answer this post in the Hazel forum.