/
exifrename
executable file
·288 lines (240 loc) · 6.33 KB
/
exifrename
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/perl -w
#
# exifrename - a perl script to rename pictures.
# Copyright (C) 2008-2013 Simon Walz, http://sw.nerdbox.de/
#
# 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 St, Fifth Floor, Boston, MA 02110, USA
use strict;
#require 5.004;
use Getopt::Long;
#use lib '/opt/local/lib/perl5/vendor_perl/5.8.8';
use Image::ExifTool qw(:Public);
sub getFN;
sub getExt;
sub getRealName;
sub isFile;
sub isInDB;
sub addToDB;
sub renamefile;
my ($dbfile, $help, $test);
my $append = "";
# default format:
my $format = "%Y_%m_%d-%H_%M_%S";
my $results = GetOptions(
"db=s" => \$dbfile,
"append=s" => \$append,
"help" => \$help,
"format=s" => \$format,
"test" => \$test
);
my @files = ();
if (defined($ARGV[0]) and $ARGV[0] eq "-") {
while(<STDIN>) {
push(@files, $_);
}
} else {
@files = @ARGV;
}
if (!(scalar @files) or defined($help)) {
print STDERR "Usage: exifrename [-db dbfile] [-append append] ".
"[-format format] [-test] files\n";
print <<EOF;
Example: exifrename *.JPG *.THM
-db dbfile - File with list of already used filenames
(new filenames will be added)
-append s - String or char to be added to the filename
-format fm - Format of the new base filename (like unix function date)
(default: %Y_%m_%d-%H_%M_%S)
-test - test, no action
- - Get files form STDIN
(c) Simon Walz, 2008-2013, http://sw.nerdbox.de/
EOF
exit(2);
}
if (defined($dbfile)) {
if (!-f $dbfile) {
print STDERR "!!! db-file not found.\n";
exit(2);
} else {
print STDERR "+++ db-file: ".$dbfile."\n";
}
}
# process all files:
foreach my $filename (@files) {
renamefile($filename, $dbfile, $append, $format, $test);
}
### Functions:
# process $filename
# rename all files with the same basename (filename without extension)
sub renamefile {
my $filename = shift;
my $dbfile = shift;
my $append = shift;
my $format = shift;
my $test = shift;
if (-d $filename) {
print STDERR "!!! Dir \"".$filename."\" ignored.\n";
return;
} elsif (!-f $filename) {
print STDERR "!!! File \"".$filename."\" not found.\n";
return;
}
my $fn = getFN($filename, $format);
# Error: no EXIF data in file
return if (!defined($fn));
my $realname = getRealName($filename);
# Error: no filename extension
return if (!defined($realname));
# check, if a character has to be added:
while (isFile($fn.$append) or isInDB($dbfile, $fn.$append)) {
if ($realname eq $fn.$append) {
print STDERR "--- New and old name are ".
"the same (".$realname.")\n";
return;
}
if ($append eq "") {
$append = "a";
} else {
# increment the last character:
$append++;
}
}
$fn .= $append;
addToDB($dbfile, $fn) if (!defined($test));
# if the file is "marked" with _m by the user or an other program
# keep this string:
if ($realname =~ /_m$/i) {
$fn .= "_m";
}
# glob() reagiert auf Sterne und Leerzeichen.
# Deshalb müssen Leerzeichen ersetzt werden.
my $filesearch = $realname.'.*';
$filesearch =~ s/ /\\ /g;
foreach my $f (glob($filesearch)) {
my $fext = getExt($f);
if (defined($fext)) {
print $f . " --> " . $fn.".".$fext."\n";
if (defined($test)) {
print "rename ".$f." ".$fn.".".$fext."\n";
} else {
rename($f, $fn.".".$fext);
}
} else {
print STDERR "--- Filename \"".$f."\" has no ".
"extension.\n";
}
}
# panorama picture:
if ($filename =~ /^ST([A-Z])/) {
open(PANORAMA, '>>', 'panorama.txt') or
die ("Could not append to 'panorama.txt'\n");
my $fext = getExt($filename);
if (defined($fext)) {
print PANORAMA $1." ".$fn.$fext;
}
print STDERR "+++ panorama picture: \"".$fn.$fext."\"\n";
close(PANORAMA) or die ("Could not write \"panorama.txt\"\n");
}
}
# get the new file-basename in defined time format
sub getFN {
my $file = shift;
my $format = shift; # default: %Y_%m_%d-%H_%M_%S
if (!defined($format) or $format eq "") {
die "!!! format of new filename not defined.\n";
}
# Use executeable version of exiftool:
#my $fn = `exiftool -CreateDate -S -d "$format" "$file"`;
#$fn =~ s/^CreateDate: //;
my $exifTool = new Image::ExifTool;
$exifTool->Options(DateFormat => $format);
my $info = $exifTool->ImageInfo($file, 'CreateDate');
if (!defined($info->{'CreateDate'})) {
print STDERR "--- No EXIF-data in file \"".$file."\" found.\n";
return undef;
}
my $fn = $info->{'CreateDate'}."\n";
chomp($fn);
return undef if ($fn eq "");
return $fn;
}
# get the extension of the file:
sub getExt {
my $file = shift;
if ($file =~ /\.([^.]+?)$/) {
$file = $1;
$file = lc $file;
return $file;
}
# Error Filename has no extension.
return undef;
}
# get the basename of the file:
sub getRealName {
my $file = shift;
# cut off the extension:
$file =~ s/\.([^.\/\\]+?)$//;
# if there is no extension -- ignore the file:
if (!defined($1)) {
print STDERR "--- Filename \"".$file."\" has no extension.\n";
return undef;
}
return $file;
}
# check, if there is a file with the same basename:
sub isFile {
my $name = shift;
my $other = "";
$name .= '.*';
$name =~ s/ /\\ /g;
my @a = glob($name);
foreach my $f (@a) {
$other .= $f;
}
return 0 if ($other eq "");
return 1;
}
# check, if the filename is in the db-file:
sub isInDB {
my $dbfile = shift;
my $name = shift;
if (!defined($dbfile) or ! -f $dbfile) {
return 0;
}
open(DB, "<", $dbfile) or die("Could not open DB.\n");
while(<DB>) {
chomp;
s/\..*?$//;
if ($_ eq $name) {
print STDERR "+++ Found in DB: ".$name."\n";
close(DB);
return 1;
}
}
close(DB);
return 0;
}
# add a filename to the db-file:
sub addToDB {
my $dbfile = shift;
my $name = shift;
if (!defined($dbfile) or ! -f $dbfile) {
return 0;
}
open(DB, ">>", $dbfile) or die("Could not open db-file for writing.\n");
print DB $name."\n";
close(DB) or die ("Could not write db-file.\n");
return 1;
}