-
Notifications
You must be signed in to change notification settings - Fork 0
/
bydate
executable file
·118 lines (100 loc) · 2.52 KB
/
bydate
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
#!/usr/bin/env perl
use File::Path qw(mkpath remove_tree);
use File::Find;
use File::stat;
use POSIX qw(strftime);
use Getopt::Long;
use Text::Tabs;
# get base program name
(my $prog = $0) =~ s!.*/(.*)$!$1!;
my %options = (
help => 0,
recurse => 0,
noDirs => 0,
verbose => 0
);
GetOptions(
"--help" => \$options{help},
"--noDirs" => \$options{noDirs},
"--recurse" => \$options{recurse},
"--verbose" => \$options{verbose}
);
my $usage = expand(<<"EOF") . "\n";
$prog [-h] [-l] [-r] [-v]
-h|--help print this verbose help
-n|--noDirs prevent symlinks to dirs
-r|--recurse only show files, recursing down the folder hierarchy
-v|--verbose print extra information
EOF
if($options{help}) {
print $usage;
exit(0);
}
my $esc = chr(27);
my $red = "${esc}[41m";
my $yellow = "${esc}[43m";
my $off = "${esc}[m";
my $ddir = "+bydate";
# remove existing "+bydate"
my $rmCount = remove_tree($ddir);
# create new "+bydate"
@dirs = mkpath(($ddir));
chmod 0755, $ddir;
if(scalar @dirs == 0) {
warn "Unable to create \"$ddir\". Exiting.\n";
exit 0;
}
my @unorderedFilelist;
if($options{recurse}) {
@unorderedFilelist = (`find . -type f`);
} else {
@unorderedFilelist = <*>;
#push @unorderedFilelist, <*/*>;
}
my @sortedFilelist = sort { -M $a <=> -M $b } @unorderedFilelist;
my $index = 1;
my $fmt = sprintf("%%0%dd", int(log10(scalar @unorderedFilelist))+1);
foreach $file (@sortedFilelist) {
chomp($file);
next if -d $file && $options{noDirs} == 1;
next if $file eq $ddir;
next if $file =~ m#^@.*#;
next if $file =~ m#^\+.*#;
next if $file =~ m#^\..*#;
# skip subtitles, playlists
next if $file =~ /.*\.(srt|idx|sub|txt|nfo|exe|m3u)$/;
$file =~ s#^\./##;
print "### $file ==> " if $options{verbose};
process($file);
}
sub process($) {
my $file = shift;
my @statinfo = stat($file);
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = stat($file);
my $str = strftime "%y%m%d", localtime($mtime);
$str = sprintf($fmt, $index);
$index++;
my $dest;
if($file =~ m#^(.*)/([^/]+)$#) {
$dest = "$ddir/$str.$2";
}
else {
$dest = "$ddir/$str.$file";
}
print "$dest\n" if $options{verbose};
if(-d $file) {
# symlink, preserving modification times
($atime, $mtime) = (stat($file))[8,9];
symlink "../$file", $dest || warn "SymLink failed: $dest\n";
# put back old update times
utime($atime, $mtime, $dest);
utime($atime, $mtime, $file);
} else {
link $file, $dest || warn "Link failed: $dest\n";
}
}
sub log10 {
my $n = shift;
return log($n)/log(10);
}