-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfile-prepend-yyyymmdd
executable file
·48 lines (41 loc) · 1.14 KB
/
file-prepend-yyyymmdd
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
#!/usr/bin/env perl
use v5.18;
use strict;
use warnings;
use File::Copy qw(move);
use File::Basename qw(dirname fileparse);
use File::Path qw(make_path);
use File::Spec::Functions qw(catfile catdir);
use File::Next;
use Getopt::Long;
sub main {
my ($opts, $args) = @_;
my @input = grep { -f || -d } @{ $opts->{i} };
my $iter = File::Next::files(@input);
my %plan;
while (defined( my $file = $iter->() )) {
my $filename = fileparse($file);
my $dirname = dirname($file);
next if (($filename =~ /\A \d{8} \- /x) || $filename =~ /\A \.DS_Store \z/x);
my $mtime = (stat($file))[9];
my ($year, $month, $day) = ( localtime($mtime) )[5,4,3];
$year += 1900; $month += 1;
my $yyyy = sprintf('%04d', $year);
my $yyyymmdd = sprintf('%04d%02d%02d', $year, $month, $day);
my $newfile = catfile($dirname, "$yyyymmdd-$filename");
if ($opts->{n}) {
say "mv $file $newfile";
} else {
move($file, $newfile);
}
}
}
my %opts;
GetOptions(
\%opts,
'i=s@',
'd=s',
'n',
'ignore-existing'
);
main(\%opts, [@ARGV]);