-
Notifications
You must be signed in to change notification settings - Fork 0
/
datedist.pl
executable file
·351 lines (321 loc) · 9.46 KB
/
datedist.pl
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/perl
# vim:ts=4:sw=4:foldmethod=marker:smartindent:ruler
# Note to vim newbies: This file uses folds. To open all folds, type zR in
# command mode.
# datedist.pl v0.2.0 docs {{{
#
# datedist.pl is a file distribution script written by Roy Sigurd Karlsbakk
# <[email protected]>. I wrote it to distribute a few thousand files in a
# directory so that they were all placed under a directory hiearchy like
# YYYYY/MM/DD to allow for me to find them later. I typically use this after a
# manual import of the pics, before importing those into Lightroom.
#
# 2013-01-30: Added --exif for using exiftool to extract shoot date instead of
# file dates, which may be different than shoot date.
#
# 2013-01-31:
# - Changed license to AGPL 3.0.
# - Added more documentation.
# - Added check to see if destination file exists to avoid overwriting existing
# stuff.
# - Added new option --force to forcibly overwrite existing files.
# - Added new option --dest-dir to allow other destination dir than current dir
# - Updated to version 0.1.2
#
# 2013-05-21:
# - Better help thanks to <[email protected]>
# - Updated to version 0.1.3
#
# 2014-02-17:
# - Code cleanup
# - Added --version
# - Updated to version 0.1.4
#
# 2016-06-13:
# - Changed to make --exif default
#
# 2016-06-14:
# - Changed old --move-corresponding-(something) to --mcf=ext,ext2 etc.
# Compatibility with old syntax is retained.
# - Version changed to 0.9.9
# - More testing to be done
#
# License: AGPL v3.0. See http://www.gnu.org/licenses/agpl-3.0.html for details.
#
# Roy Sigurd Karlsbakk <[email protected]>
#
# }}}
# Uses {{{
use strict;
use warnings;
use File::Copy;
use File::Path qw(make_path);
use Getopt::Long;
use Image::ExifTool qw(:Public);
use Time::Format qw(%strftime);
use Data::Dumper;
# }}}
# Globals and settings {{{
# Flags
my $no_day_dir = 0;
my $no_month_dir = 0;
my $hour_dir = 0;
my $minute_dir = 0;
my $norun = 0;
my $help = 0;
my $verbose = 0;
my $exif = 1;
my $noexif = 0;
my $mcf = undef;
my $move_corresponding_xmp = 0;
my $move_corresponding_jpg = 0;
my $move_corresponding_png = 0;
my $move_corresponding_tif = 0;
my $move_corresponding_files = 0;
# stuff
my %mcf_exts;
my $force = 0;
my $print_version = 0;
# Strings
my $dest_dir = undef;
my $version = '0.1.4';
# }}}
# sub help {{{
sub help
{
print <<EOT;
Syntax: $0 [ opts ] filename [ filename [ ... ] ]
--dest-dir Distribute files under given dir, not current
directory.
--hour Create hourly dirs
--minute Create minutely dirs (implies --hour)
--nomonth Do not create monthly dirs (implies --noday)
--noday Do not create daily dirs
--exif Distribute files by shoot time in exif data instead
of file date. Will post a warning and ignore files
without exif data. (default)
--noexif Negates exif (above).
--move-corresponding-xmp Moves corresponding xmp too
--move-corresponding-jpg Moves corresponding jpg too
--move-corresponding-tif Moves corresponding tif too
--force Forcely overwrite existing files
--help Display this help
--version Show version number and exit
--nomonth/--noday are incompatible with --hour/--minute
EOT
exit (1);
}
# danke :) :)
# }}}
# sub syntax {{{
sub syntax
{
print "wtf?\n"
}
# }}}
# sub version {{{
sub version
{
print "datedist.pl version $version\n";
exit 0;
}
# }}}
# Parse options {{{
Getopt::Long::Configure('bundling');
GetOptions(
"dest-dir=s" => \$dest_dir,
"noday" => \$no_day_dir,
"nomonth" => \$no_month_dir,
"hour" => \$hour_dir,
"minute" => \$minute_dir,
"norun" => \$norun,
"exif" => \$exif,
"noexif" => \$noexif,
"mcf=s" => \$mcf,
"move-corresponding-xmp" => \$move_corresponding_xmp,
"move-corresponding-jpg" => \$move_corresponding_jpg,
"move-corresponding-png" => \$move_corresponding_png,
"move-corresponding-tif" => \$move_corresponding_tif,
"move-corresponding-files" => \$move_corresponding_files,
"force" => \$force,
"verbose+" => \$verbose,
"version" => \$print_version,
"help" => \$help,
) or syntax("Illegal option!");
$no_day_dir = 1 if ($no_month_dir);
$hour_dir = 1 if ($minute_dir);
&help("Incompatible options!") if ($no_day_dir and $hour_dir);
&help if ($help);
&version if ($print_version);
$exif=0 if ($noexif);
# Move corresponding files - new way {{{
# mcf is "move corresponding files" the new way
if (defined($mcf)) {
chomp($mcf);
foreach my $ext (split $mcf,',') {
$mcf_exts{$ext}++;
}
}
# }}}
# Backward compatibility {{{
$move_corresponding_xmp = $move_corresponding_jpg = $move_corresponding_png = $move_corresponding_tif = 1 if ($move_corresponding_files);
if ($move_corresponding_xmp) {
$mcf_exts{'xmp'}++;
}
if ($move_corresponding_jpg) {
$mcf_exts{'jpg'}++;
$mcf_exts{'jpeg'}++;
}
if ($move_corresponding_png) {
$mcf_exts{'png'}++ ;
}
if ($move_corresponding_tif) {
$mcf_exts{'tiff'}++ ;
}
# }}}
# }}}
# Main loop {{{
while (my $filename = shift)
{
my ($mtime,$year,$month,$day,$hour,$minute,$second);
unless (-f $filename && -r $filename)
{
warn "$filename is not a file or is not readable. Skipping\n";
next;
}
if ($exif) {
my $exifinfo = ImageInfo($filename);
unless (defined($exifinfo)) {
print STDERR "Ignoring $filename - undefined exifinfo\n";
next;
}
my $exifdatetime = $exifinfo->{'DateTimeOriginal'};
# sånn - hvis den ikke kjenner igjen exifdataene, så driter den i fila. ok ok
print "Photo taken $exifdatetime\n" if ($verbose);
#if (/2007:02:23 22:53:45
if (defined($exifdatetime) and $exifdatetime =~ /^(\d{4}):(\d{2}):(\d{2}) (\d{2}):(\d{2}):(\d{2})$/) {
($year,$month,$day,$hour,$minute,$second) = ($1,$2,$3,$4,$5,$6);
} else {
print STDERR "Can't read exif data from file $filename\n";
next;
}
} else {
$mtime = (stat($filename))[9];
$year = $strftime{'%Y', $mtime};
$month = $strftime{'%m', $mtime};
$day = $strftime{'%d', $mtime};
$hour = $strftime{'%H', $mtime};
$minute = $strftime{'%M', $mtime};
$second = $strftime{'%S', $mtime};
}
# Create directory tree
my $dir;
if (defined($dest_dir)) {
$dir = $dest_dir;
$dir .= '/' unless ($dir =~ /\/$/);
}
$dir .= "$year";
$dir .= "/$month" unless ($no_month_dir);
$dir .= "/$day" unless ($no_day_dir);
$dir .= "/$hour" if ($hour_dir);
$dir .= "/$minute" if ($minute_dir);
make_path($dir, {
verbose => $verbose,
mode => 0755,
});
# If $filename has directory names in it, remove them in $destfilename
# my $destfilename = $filename;
# $destfilename = $1 if ($destfilename =~ /.*\/(.*)/);
# Move file
if (-f "$dir/$filename" && ! $force) {
print "Skipping file $filename (exists in $dir already)\n";
next;
}
if (move($filename,"$dir/$filename"))
{
# det er denne blokka du vil kopiere og endre. ok bruk stor V - så merker den hele linjer.
# k
if ($move_corresponding_jpg)
{
my $jpg_filename = $filename;
$jpg_filename =~ s/(.*?)\.\w+$/$1.jpg/i;
unless (-f $jpg_filename) {
print "Can't find jpg file '$jpg_filename'\n" if ($verbose);
$jpg_filename = "$filename.jpg";
print "Trying '$jpg_filename' for $filename\n" if ($verbose);
}
if (-f $jpg_filename) {
if (move($jpg_filename,"$dir/$jpg_filename")) {
my $jpg_xfs_filename = "$jpg_filename.xmp";
if ( -f $jpg_xfs_filename ) {
move($jpg_xfs_filename,"$dir/$jpg_xfs_filename");
}
}
} else {
print "Can't find jpg file '$jpg_filename', ignoring it (about $filename)\n" if ($verbose);
}
}
if ($move_corresponding_png)
{
my $png_filename = $filename;
$png_filename =~ s/(.*?)\.\w+$/$1.png/i;
unless (-f $png_filename) {
print "Can't find png file '$png_filename'\n" if ($verbose);
$png_filename = "$filename.png";
print "Trying '$png_filename' for $filename\n" if ($verbose);
}
if (-f $png_filename) {
if (move($png_filename,"$dir/$png_filename")) {
my $png_xfs_filename = "$png_filename.xmp";
if ( -f $png_xfs_filename ) {
move($png_xfs_filename,"$dir/$png_xfs_filename");
}
}
} else {
print "Can't find png file '$png_filename', ignoring it (about $filename)\n" if ($verbose);
}
}
if ($move_corresponding_tif)
{
my $tif_filename = $filename;
$tif_filename =~ s/(.*?)\.\w+$/$1.tif/i;
unless (-f $tif_filename) {
print "Can't find tif file '$tif_filename'\n" if ($verbose);
$tif_filename = "$filename.tif";
print "Trying '$tif_filename' for $filename\n" if ($verbose);
}
if (-f $tif_filename) {
if (move($tif_filename,"$dir/$tif_filename")) {
my $tif_xfs_filename = "$tif_filename.xmp";
if ( -f $tif_xfs_filename ) {
move($tif_xfs_filename,"$dir/$tif_xfs_filename");
}
}
} else {
print "Can't find tif file '$tif_filename', ignoring it (about $filename)\n" if ($verbose);
}
}
if ($move_corresponding_xmp)
{
my $xmp_filename = $filename;
$xmp_filename =~ s/(.*?)\.\w+$/$1.xmp/i;
unless (-f $xmp_filename) {
print "Can't find xmp file '$xmp_filename'\n" if ($verbose);
$xmp_filename = "$filename.xmp";
print "Trying '$xmp_filename' for $filename\n" if ($verbose);
}
if (-f $xmp_filename) {
move($xmp_filename,"$dir/$xmp_filename") if (-f $xmp_filename);
} else {
print "Can't find xmp file '$xmp_filename', ignoring it (about $filename)\n" if ($verbose);
}
}
} else {
warn "Unable to move $filename to $dir\n" unless (move($filename,"$dir/$filename"));
}
}
# }}}
#
# finito
#
# finitato