Bjoern Olausson

camrename
Tuesday, 26 May 2009 19:08

camrename Version:1.0

Rename a picture taken with the Digital Camera File specification to
use a more Unix-friendly name.

Each given file is renamed. If the existing name begins or ends in
four digits (preference given to ending), those four digits will be
preserved in the new name as "seq"; otherwise "seqn" will be set to
ensure a unique filename, starting at 0001 or one more than the
largest existing sequence number. The remainder of the name will be
created as follows:

Pyyyy.mm.dd-hh.mm.ss-seqn.jpg

 

Original script from "Geoff's Digicam Tools for Linux"
(http://lasr.cs.ucla.edu/geoff/digicam/)
Modified by me (Bjoern Olausson) to work with exiftool instead of Metacam

 GNU/GPL    2011-03-14   English   Linux  3.76 KB  112

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/sh
#
# $Id$
#
# Rename a picture taken with the Digital Camera File specification to
# use a more Unix-friendly name.
#
# Usage:
#
USAGE='Usage: camrename [-d] [-v] jpeg-files'
#
# Each given file is renamed.  If the existing name begins or ends in
# four digits (preference given to ending), those four digits will be
# preserved in the new name as "seq"; otherwise "seqn" will be set to
# ensure a unique filename, starting at 0001 or one more than the
# largest existing sequence number.  The remainder of the name will be
# created as follows:
#
#	Pyyyy.mm.dd-hh.mm.ss-seqn.jpg
#	 ^^^^ ^^ ^^ ^^ ^^ ^^ ^^^^
#	  |   |  |  |  |  |   |
#	  |   |  |  |  |  |   +--- Image sequence number
#	  |   |  |  |  |  |
#	  |   |  |  |  |  +------- Seconds of image creation date/time
#	  |   |  |  |  |
#	  |   |  |  |  +---------- Minutes of image creation date/time
#	  |   |  |  |
#	  |   |  |  +------------- Hours of image creation date/time
#	  |   |  |
#	  |   |  +---------------- Day of month image created
#	  |   |
#	  |   +------------------- Month image created
#	  |
#	  +----------------------- Year image created
#
#
# As a side effect, to compensate for MS-DOS stupidity, the execute
# permissions are turned off on all renamed files.
#
# Options:
#
# -d		Debug: report what would be done, but don't do it.
# -v		Verbose: report each rename as it is done.
#
 
# Original script from "Geoff's Digicam Tools for Linux"
# (http://lasr.cs.ucla.edu/geoff/digicam/)
# Modified by me (Bjoern Olausson) to work with exiftool instead of Metacam
 
debug=false
verbose=false
 
#
# Argument processing
#
while [ $# -gt 0 ]
do
    case "$1" in
	-d)
	    debug=true
	    verbose=true
	    ;;
	-v)
	    verbose=true
	    ;;
	--)
	    shift
	    break
	    ;;
	-*)
	    echo "$USAGE" 1>&2
	    exit 2
	    ;;
	*)
	    break
	    ;;
    esac
    shift
done
 
case "$#" in
    0)
	echo "$USAGE" 1>&2
	exit 2
	;;
esac
 
#
# Process all file arguments
#
TMP=/tmp/cre$$
 
trap "rm -f $TMP.*; exit 1" 1 2 15
trap "rm -f $TMP.*; exit 0" 13
 
for file ; do
	dir=`dirname $file`
	base=${file%.*}
	origExtension=${file##*.}
	extension=$(tr "A-Z" "a-z" <<< $origExtension)
    #
    # Extract file information with metacam.
    #
    if ! exiftool -CreateDate "$file" > $TMP.a 2>&1
    then
	echo "Can't extract exif-information from '$file', skipping" 1>&2
	continue
    fi
    datePart=`awk '/Create Date/{print $4" "$5}' $TMP.a | sed -e 's/ /-/;s/:/./g'`
    #
    # Figure out new name, with *lots* of safety checks.
    #
    case "$base" in
	*[0-9][0-9][0-9][0-9])
	    seq=`expr "$base" : '.*\([0-9][0-9][0-9][0-9]\)$'`
	    ;;
	[0-9][0-9][0-9][0-9]*)
	    seq=`expr "$base" : '\([0-9][0-9][0-9][0-9]\).*$'`
	    ;;
	*)
	    topFile=`ls $dir/P$datePart-*.$extension 2>/dev/null | tail -1`
	    case "$topFile" in
		"$dir/P$datePart-*.$extension")
		    seq=0001
		    ;;
		*)
		    seq=`expr "$topFile" \
		      : ".*/P$datePart-\([0-9][0-9][0-9][0-9]\).$extension"'$'`
		    if [ "X$seq" = X ]
		    then
			seq=0001
		    fi
		    ;;
	    esac
	    while [ -e "$dir/P$datePart-$seq.$extension" ]
	    do
		seq=`echo $seq | awk '{printf "%4.4d\n", $1 + 1}'`
		if  [ $seq -gt 9999 ]
		then
		    echo "Can't find unique sequence number to rename $file " \
		      "to $dir/P$datePart-*.$extension; not renamed" 1>&2
		    continue
		fi
	    done
	    ;;
    esac
    target="$dir/P$datePart-$seq.$extension"
    if [ "$target" = "$dir/$base.$origExtension" ]
    then
	# Already in proper format
	echo "$base is already in proper format; skipped" 1>&2
	continue
    elif [ -e "$target" ]
    then
	echo "$target already exists; $file not renamed" 1>&2
	continue
    fi
    #
    # Finally, we're ready to do the rename.
    #
    $verbose  &&  echo mv "$file" "$target"
    if ! $debug
    then
	mv "$file" "$target"
	chmod 644 "$target"
    fi
done
 
rm -f $TMP.*

 

Last Updated ( Monday, 14 March 2011 09:04 )
 

Add comment


Security code
Refresh

Comments

Qt Ambassador

Qt Ambassador

www. is deprecated

Banner

Play OGG

Banner

Gixen

web2sms

Banner