2 # archive_photos archives photos into a directory structure
3 # and is released under the terms of the GNU GPL version 3, or any
4 # later version, at your option. See the file README and COPYING for
6 # Copyright 2013 by Don Armstrong <don@donarmstrong.com>.
17 archive_photos - archives photos into a directory structure
21 archive_photos [options] path/to/photos [additional photos]
24 --recurse, -r recurse into subdirectories (default)
25 --archive-dir, -a directory to archive into (~/media/photos)
26 --debug, -d debugging level (Default 0)
27 --help, -h display this help
28 --man, -m display manual
34 =item B<--recurse, -r>
36 Recurse into subdirectories (default; use --no-recurse to disable
38 =item B<--archive-dir, -a>
40 Directory to archive to (default is ~/media/photos)
44 Debug verbosity. (Default 0)
48 Display brief usage information.
66 use Image::ExifTool qw(ImageInfo);
67 use POSIX qw(strftime);
69 use File::Path qw(make_path);
71 use Date::Parse qw(str2time);
73 my %options = (debug => 0,
76 archive_dir => User->Home."/media/photos",
81 'archive_dir|archive-dir=s',
83 'debug|d+','help|h|?','man|m');
85 pod2usage() if $options{help};
86 pod2usage({verbose=>2}) if $options{man};
88 $DEBUG = $options{debug};
92 push @USAGE_ERRORS,"You must give at least one directory";
95 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
99 # loads files into @files as appropriate
101 if (-d $_ and not $options{recurse}) {
102 $File::Find::prune = 1;
105 if (-f $_ and $_ =~ /\.(?:NEF|JPG|MOV)$/i) {
106 push @files,$File::Find::name;
112 for my $arg (@ARGV) {
120 # search all of the directories and store the files in @files;
121 find(\&file_pusher,@dirs) if @dirs;
123 # find already existing files
125 find(sub { if (-f $_) {$existing_files{$_} = $File::Find::name;} },$options{archive_dir});
127 for my $file (@files) {
128 my $file_basename=basename($file);
129 # find out when the photo was shot
130 my $info = ImageInfo($file);
131 if (not defined $info->{CreateDate}) {
132 print STDERR "No date information for $file\n";
135 my $epoch = str2time($info->{CreateDate});
136 my $dir = strftime('%Y/%m_%B/%Y_%m_%d/',localtime($epoch)).'orig/';
137 if (not -d $options{archive_dir}.'/'.$dir) {
138 make_path($options{archive_dir}.'/'.$dir) or
139 die "Unable to make dir $!";
141 my $end_location = $options{archive_dir}.'/'.$dir.$file_basename;
142 if (-e $end_location) {
143 print STDERR "$file already exists in $end_location\n";
144 } elsif (exists $existing_files{$file_basename}) {
145 print STDERR "$file already exists in $existing_files{$file_basename}\n";
147 print STDERR "copying $file to $end_location\n";
148 copy($file,$end_location);