Add name-by-date post.
authorW. Trevor King <wking@drexel.edu>
Tue, 1 Feb 2011 16:40:50 +0000 (11:40 -0500)
committerW. Trevor King <wking@drexel.edu>
Tue, 1 Feb 2011 16:40:50 +0000 (11:40 -0500)
posts/name-by-date.mdwn [new file with mode: 0644]
posts/name-by-date/name-by-date.sh [new file with mode: 0755]

diff --git a/posts/name-by-date.mdwn b/posts/name-by-date.mdwn
new file mode 100644 (file)
index 0000000..dd259b4
--- /dev/null
@@ -0,0 +1,16 @@
+[[name-by-date.sh]] is a [[bash]] script to rename JPEGs to show their
+creation date.
+
+    $ name-by-date.sh *.JPG
+    DSC_0350.JPG taken on "2010.07.02.08.36.20"
+    DSC_0356.JPG taken on "2010.07.02.08.38.10"
+    ...
+
+`name-by-date.sh` uses Daniel Stephens' [MetaCam][] (ebuild in my
+[[Gentoo overlay]]) to extract thed ate from the picture's metadata,
+so you'll have to install that first.
+
+[metacam]: http://www.cheeseplant.org/~daniel/pages/metacam.html
+
+[[!tag tags/bash]]
+[[!tag tags/programming]]
diff --git a/posts/name-by-date/name-by-date.sh b/posts/name-by-date/name-by-date.sh
new file mode 100755 (executable)
index 0000000..1f3f276
--- /dev/null
@@ -0,0 +1,54 @@
+#!/bin/bash
+#
+# Rename JPEGs to reflect the creation date stored in their metadata.
+#
+# Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+#
+# usage: name-by-date.sh $(find . -name '*.jpg')
+#
+# uses metacam, [, cut, dirname, echo, file, grep, let, mv, and sed.
+
+
+while [ -n "$1" ]; do
+       FILE=$1
+       TYPE=$(file "$FILE" | grep JPEG)
+       if [ -n "$TYPE" ]; then
+               DATE_LINE=$(metacam "$FILE" | grep 'Image Creation Date')
+               DATE=$(echo "$DATE_LINE" | cut -d: -f 2,3,4,5,6 | sed 's/:/./g' | cut -d\  -f2,3 | sed 's/ /./g')
+               if [ -z "$DATE" ]; then
+                       echo "$FILE has no stored date"
+               else
+                       echo "$FILE taken on \"$DATE\""
+                       FILE_DIR=$(dirname "$FILE")
+                       NEW_NAME="$FILE_DIR/$DATE.jpg"
+                       if [ -e "$NEW_NAME" ] && [ "$NEW_NAME" != "$FILE" ]; then
+                               i=1
+                               NEW_NAME="$FILE_DIR/${DATE}_$i.jpg"
+                               while [ -e "$NEW_NAME" ] && [ "$NEW_NAME" != "$FILE" ]; do
+                                       echo "File \"$NEW_NAME\" already exists!"
+                                       NEW_NAME="$FILE_DIR/${DATE}_$i.jpg"
+                                       let "i += 1"
+                               done
+                       fi
+                       if [ "$NEW_NAME" != "$FILE" ]; then
+                               mv "$FILE" "$NEW_NAME"
+                       fi
+               fi
+       fi
+       shift
+done