2009년 9월 2일

[perl] 파일의 생성시간과 현재 시간을 비교

#!/usr/bin/perl

 

 

use strict;

 

my $conv_imgae_root_dir = "/home/sjang/mydir1";

my $ori_imgae_root_dir = "/home/sjang/mydir2";

 

# expire time: 14days

my $expire_seconds = 10080*60;

 

my $current_time = time;

my $current_hour;

 

while(1)

{

        $current_hour = get_hour();

        if ($current_hour eq "04") # every dat 04:xx

        {

               # start to check...

               # print "$current_hour, 4 si\n";

               check_expire($conv_imgae_root_dir);

               check_expire($ori_imgae_root_dir);

        }

        # per hour

        sleep 60*60;

}

 

sub check_expire()

{

        my $check_dir = $_[0];

        my @files;

 

        #print "start ...$check_dir \n";

 

        opendir(DIR, $check_dir);

        @files = readdir(DIR);

        closedir(DIR);

         

         

        # build a unsorted list from the

        # # @files array:

 

        my $file;

        foreach $file (@files)

        {

               my $working_dir;

               my $ab_file;

               next if ($file eq "." or $file eq "..");

#              print "- $check_dir\/$file\n";

               $ab_file = "$check_dir/$file";

               if (-d $ab_file)

               {

#                      print "working dir = $ab_file\n";

                       check_expire($ab_file);

               }

               else

               {

                       my $file_time = get_file_time($ab_file);

                       my $diff_time = $current_time - $file_time;

                       if ( $diff_time > $expire_seconds)

                       {

#                             print "file remove = $ab_file\n";

                              system("rm $ab_file");

                       }

               }

        }

}

 

sub get_file_time()

{

        my $filename = $_[0];

        my $mtime;

        $mtime = (stat($filename))[9];

        return $mtime;

}

 

sub get_hour()

{

        my $today_time;

        my $day;

        my $month;

        my $year;

        my $hour;

        my $minutes;

        my $seconds;

 

        ($seconds, $minutes, $hour, $day, $month, $year) = (localtime)[0, 1, 2, 3, 4, 5];

        #($day, $month, $year) = (localtime)[3, 4, 5];

        $year += 1900;

        $month += 1;

        if ( $month < 10 )

        {

               $month = "0".$month;

        }

        if ( $day < 10 )

        {

               $day = "0".$day;

        }

        if ( $hour < 10 )

        {

               $hour = "0".$hour;

        }

        if ( $minutes < 10 )

        {

               $minutes = "0".$minutes;

        }

        if ( $seconds < 10 )

        {

               $seconds = "0".$seconds;

        }

 

        return $hour;

}

댓글 없음:

댓글 쓰기