#BY Weigun http://wfnh.cublog.cn/
use strict; use Config::IniFiles; use Date::Simple; $|=1; my $config_file='conf.ini'; my $cfg=&load_config($config_file); #$cfg is a ref of hash
my $result=&search_file($cfg->{root}); #print $_,"\n" for(@$result);
scalar @$result?&write_file($result):exit; &delete_all;
#------------主程序结束-----------------------
sub load_config { my $cfg_file=shift; my $cfg={}; my $conf=Config::IniFiles->new( -file => $cfg_file); die "can't find $cfg_file:$!\n" if !$conf; $cfg->{root}=lc $conf->val('CHECK_POINT','ROOT'); $cfg->{white_list}->{dir}=lc $conf->val('WHITE_LIST','DIR'); $cfg->{white_list}->{file_name}=lc $conf->val('WHITE_LIST','FileName'); $cfg->{limit}=$conf->val('TIME_LIMIT','DAYS'); return $cfg; }
sub search_file { my @files; my @dir=(shift); while(my $dir=pop @dir) { next unless opendir DIR,$dir; #print $dir,"\n";
foreach my $file(readdir DIR) { next if $file eq '.' or $file eq '..'; my $path=$dir.$file; if(-d $path) { next if grep $file eq $_,split / /,$cfg->{white_list}->{dir}; push @dir,$path.'\\'; next; } if(-f _) #$path
{ next if grep $file eq $_,split / /,$cfg->{white_list}->{file_name}; push @files,$path if check_create_time($path); } } } closedir DIR; return \@files; }
sub check_create_time { my $file=shift; my $ctime=&format_date((stat $file)[10]); my $today = Date::Simple->new; ($today-Date::Simple->new($ctime)) >$cfg->{limit}?return 1:return; } sub format_date { #Nov 12
my $date_string=shift; my ($moon,$date,$year)=(split /\s+/,scalar localtime $date_string)[1,2,-1]; my %moon=( 'Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12', ); #my $year=(localtime)[5]+1900;
#$year+=1900;
$moon=$moon{$moon}; $date='0'.$date if length $date <2; return $year.'-'.$moon.'-'.$date; }
sub write_file { my $ref_result=shift; my $file='result.txt'; open OUT,">",$file; print OUT $_,"\n" for(@$ref_result); close OUT||die "can't close file_handle:$!\n"; }
sub delete_all { my $file='result.txt'; open IN,"<",$file; while(<IN>) { chomp; if(-e) { print $_," will be delete\n"; #unlink ||next;
} } close IN||die "can't close file_handle:$!\n"; }
|