-
Notifications
You must be signed in to change notification settings - Fork 6
/
iphoto-move-export-to-archive-dir
executable file
·55 lines (43 loc) · 1.48 KB
/
iphoto-move-export-to-archive-dir
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
#!/usr/bin/perl
use strict;
my $help = <<_HELP;
Usage: $0 ARCHIVE_ROOT FILES ...
_HELP
my $archive = shift @ARGV || die $help;
chomp(@ARGV = <STDIN>) unless @ARGV;
my %ARCHIVE_DIRS;
for my $from (@ARGV) {
if ($from !~ m@((.*)/)?([^/]+)$@) {
print STDERR "-F|Cannot parse filename $from.";
die;
}
my ($dir, $file ) = ($2,$3);
my $mtime = (stat $from)[9];
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime);
$year += 1900;
$mon += 1;
my $archiveYear = sprintf("%s/%04d", $archive, $year);
my $archiveDir = sprintf("%s/%04d-%02d-%02d", $archiveYear, $year, $mon, $mday);
if (! defined($ARCHIVE_DIRS{$archiveDir})) {
$ARCHIVE_DIRS{$archiveDir} = 1;
if ( ! -d $archiveYear) {
mkdir $archiveYear or die "Cannot create dir $archiveYear: $!";
}
if ( ! -d $archiveDir) {
mkdir $archiveDir or die "Cannot create dir $archiveDir: $!";
}
}
my $to = sprintf("%s/%s", $archiveDir, $file);
unless (-f $to) {
system("mv -vf '$from' '$to'");
# system("touch -mac -r '$from' '$to'");
} else {
my $sourceSize = -s $from;
my $archiveSize = -s $to;
if ($sourceSize != $archiveSize) {
print STDERR "-W|File '$to' already exists. Sizes differ (src $sourceSize, target $archiveSize). Skipping copy.\n";
} else {
print STDERR "-W|File '$to' already exists. Same sized. Skipping copy.\n";
}
}
}