Selvakumar has asked for the wisdom of the Perl Monks concerning the following question:

Can anybody suggest where i am wrong to check the files older than three days. I am using the below code. Find the files details below and modified time of the files details also.
use strict; use warnings; my $today = time; my @rootfolders=("d:/temp/"); for my $folder (@rootfolders) { $folder=~s/\\/\//g; $folder=$folder."/"; $folder=~s/([\/]+)$/\//; opendir(DIR,"$folder"); for my $temp (readdir(DIR)) { if (!(($temp eq ".") || ($temp eq ".."))) { print "$folder$temp"; my $temp1=$folder.$temp; my $mtime = (stat "$temp1") [9]; my ($sec,$min,$hour,$day,$month,$year) = localtime($mtime); $month++; print " $month/$day/$year $hour:$min "; #I am trying to find here 12:00 AM in the morning time.so the files sh +ould be exactly three days back. ($sec,$min,$hour,$day,$month,$year) = localtime($today); if ($hour > 1) { $hour=$hour*60*60; } my $findtime=$sec+$min+$hour; $today=$today-$findtime; my $older=(60*60*24)*3; $today=$today-$older; ($sec,$min,$hour,$day,$month,$year) = localtime($today); $month++; if ($today > $mtime) { print " OLDER 3 days"; } if (-d $temp1) { print " is a folder\n"; } else { print " is a FILE\n"; } } } close(DIR); }
d:/temp/BBDPS0478M_ITR.pdf 7/30/109 17:13 OLDER 3 days is a FILE d:/temp/Binder1.pdf 6/12/109 19:20 OLDER 3 days is a FILE d:/temp/bri.doc 8/1/109 15:32 is a FILE d:/temp/Can be delete.xls 2/18/109 16:4 OLDER 3 days is a FILE d:/temp/doc 8/3/109 15:52 is a folder d:/temp/Docx 8/3/109 15:52 is a folder d:/temp/ps 8/3/109 15:52 is a folder d:/temp/screenshot.bmp 7/31/109 17:51 is a FILE

Replies are listed 'Best First'.
Re: older files
by moritz (Cardinal) on Aug 04, 2009 at 06:37 UTC

    Your problem is that you're trying to re-invent a well written and well tested wheel, and get it wrong along the way.

    I recommend using DateTime, Data::Calc or a similar module to do your calculation.

    (This here looks very suspicious, just look at the units associated (notionally) with each variable:

    my $findtime=$sec+$min+$hour;

    But fixing it not worth the effort. The modules can do the date calculations much better).

Re: older files
by merlponk (Scribe) on Aug 04, 2009 at 06:59 UTC
    Am I missing something? Why not just do:
    my $three_days = 86400 * 3; my $mtime = (stat $temp1)[9]; if (time - $mtime > $three_days) { print " OLDER 3 days"; }
      i tried that and it's not worked for me. I don't know why. I got the solution using the below way. Thanks for the reply.
      my $mtime = (stat "$temp1") [9]; my $older=60*60*24; my $diff=$today-$mtime; $diff=$diff/$older; print " DIFF $diff "; if ($diff >3)

        No need for the date calculation, the -M operator already does that for you:

        print "Old\n" if -M $temp1 > 3;
        -derby