-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime2filename.pl
executable file
·157 lines (137 loc) · 4.35 KB
/
time2filename.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
#!/usr/bin/perl
#
# this programm uses Image::ExifTool by Phil Harvey
# download and install it: http://www.sno.phy.queensu.ca/~phil/exiftool/
use File::Copy;
use Image::ExifTool 'ImageInfo';
use File::Basename;
use File::stat;
use Time::localtime;
use Date::Parse;
use DateTime;
use constant FALSE => 0;
use constant TRUE => 1;
# no arguments given? print help and exit
if ($#ARGV == -1) {
print_help();
exit(1);
}
# variables for storing command line arguments
my $dontcheckarguments = FALSE;
my $verbose = FALSE;
my $pretend = FALSE;
my $adjust = 0;
for my $fromfile (@ARGV) {
# check for arguments
if ($dontcheckarguments == FALSE) {
if ($fromfile eq "-h") {
print_help();
next;
} elsif ($fromfile eq "-v") {
$verbose = TRUE;
next;
} elsif ($fromfile eq "-p") {
$pretend = TRUE;
$verbose = TRUE;
next;
} elsif ($fromfile =~ /^-a-?\d*/) {
$adjust = substr($fromfile, 2);
next;
} elsif ($fromfile eq "--") {
$dontcheckarguments = TRUE;
next;
}
}
# get exif creation timestamp of file
my $info = ImageInfo($fromfile);
my $estamp = $info->{"DateTimeOriginal"};
my $esecs; # seconds since epoch
# if no timestamp in file -> use other info
if (length($estamp) != 19) {
if ($fromfile =~ /^VID_\d{8}_\d{6}\..*$/) {
# Android Video: timestamp is in filename
print "No EXIF Timestamp. Using date+time from filename: $fromfile\n";
$timestring = $fromfile;
$timestring =~ s/VID_(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})\..*$/\1-\2-\3 \4:\5:\6/;
$esecs = str2time($timestring);
} else {
# -> use mod-time of file
print "No EXIF Timestamp. Using modification time: $fromfile\n";
$esecs = stat($fromfile)->mtime;
}
} else {
$esecs = str2time($estamp);
}
# eventually adjust time
$esecs += $adjust;
# prepare timestamp
my $timestamp = sprintf "%4d-%02d-%02d %02d-%02d-%02d",
localtime($esecs)->year+1900,
localtime($esecs)->mon+1,
localtime($esecs)->mday,
localtime($esecs)->hour,
localtime($esecs)->min,
localtime($esecs)->sec;
$timestamp =~ s/:/-/g;
# cut fromfile in file's name and dir name
$filename = basename($fromfile);
$dirname = dirname($fromfile);
# strip existing timestamp from beginning of fromfile
# (formats checked are: NNNN.NN.NN and NNNN.NN.NN.NN.NN.NN
# where N is a digit and . is no digit)
$newname = $filename;
$newname =~ s/^[0-9]{4}([^0-9][0-9]{2}){2}(([^0-9][0-9]{2}){3})?//;
# strip delimiter characters (-_ ) from beginning of fromfile
$newname =~ s/^(-|_| )*//;
# check if the new name would be the same as the old name
if ($filename eq "$timestamp $newname") {
if ($verbose == TRUE) {
print "name is OK, doing nothing: $fromfile\n";
}
next;
}
# rename the file
# counter for adding a number, if the file name already exists
$try = 0;
while (1) {
# build target fromfile
if ($try == 0) {
$filenumber = "";
} else {
$filenumber = "-" . $try;
}
$tofile = "$dirname/$timestamp$filenumber $newname";
# test if file already exists
if (-e "$tofile") {
# increase number and do the loop again
$try = $try + 1;
next;
}
# move file
if ($verbose == TRUE) {
if ($pretend == TRUE) {
print "not ";
}
print "renaming '$fromfile' to '$tofile'\n";
}
if ($pretend == FALSE) {
move($fromfile, $tofile);
}
last;
}
}
sub print_help {
print "time2filename.pl will add prepend a files name with a timestamp.\n";
print "If present it will use the EXIF-Timestamp included in the file.\n";
print "If not it will use the files modification time.\n";
print "\n";
print "command line arguments:\n";
print " -p just pretend to rename files, do nothing (implies -v)\n";
print " -a<sec> adjust time by <sec> seconds. can be negative\n";
print " -v be verbose\n";
print " -- stop arguments scanning (for files named -v or the like)\n";
print " -h print this help\n";
print "\n";
print "hint: to recurse into subdirectories use\n";
print " find <pathname ...> -exec time2filename.pl -p {} \\;\n";
}