Chinaunix首页 | 论坛 | 博客
  • 博客访问: 128228
  • 博文数量: 28
  • 博客积分: 2431
  • 博客等级: 大尉
  • 技术积分: 321
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-29 18:40
文章分类

全部博文(28)

文章存档

2011年(3)

2010年(6)

2009年(7)

2008年(12)

分类:

2009-12-14 15:18:51

服务器开通了FTP上传,所以,很多时候,一些放很久的文件会占比较多的空间,这个脚本就是ftp文件在服务器上保留多少天后就自动删除的小脚本。这个只做了基本功能,没有深入去做,反正我这里就够用。win2003通过
功能:
1.要求自动删除过期的文件,例如存放超过10天的自动删除
2.可以设置白名单,某些目录或者文件例外
3.生成删除文件列表。
code:
 

#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";
}
    
    


阅读(1830) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~