perl怎么从一个目录里抓取最近的文件
1个回答
展开全部
my @res = filter_dir(".", "<10day");
print "@res";
sub to_sec {
my %k = ('day' => 3600*24,
'mon' => 3600*24*30,
'week' => 3600*24*7,
'year' => 3600*24*365,
'hour' => 3600, 'min' => 60, 'sec' => 1);
return $k{(shift)} || 1;
}
sub filter_dir {
my ($dir, $logic) = @_;
my $D;
my @logic;
opendir $D, $dir || return;
chdir $dir;
my @f = grep {-f} readdir $D;
close $D;
my $cur = time;
while ($logic =~/([><]?[=]?)\s*(\d+\.?\d*)\s*(\w+)/g) {
my $sec = $2* to_sec $3;
my %cmp = (">" => sub {$cur - $_[0] > $sec},
">=" => sub {$cur - $_[0] >= $sec},
"<" => sub {$cur - $_[0] < $sec},
"<=" => sub {$cur - $_[0] <= $sec},
"=" => sub {$cur - $_[0] == $sec},
);
push @logic, $cmp{$1};
}
my @m = map {(stat)[9]}@f;
my @res;
if (@logic) {
O: for my $i(0..$#m) {
for my $cmp(@logic) {
next O if (! &$cmp($m[$i]));
}
push @res, $f[$i];
}
}
return @res;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询