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

Can some one show me how I capture mtime so I can compare it to the current time. I want to loop through a directory of files and delete the ones that are more than 3 days old.
sub clean_up{ my ($dir) = @_; my @files = (); my ($seconds, $min, $hour); my $time = timelocal($seconds, $min, $hour, (localtime)[3,4,5]); find sub { push @files, $File::Find::name; }, $dir; @files = sort @files; foreach my $file (@files){ my ( $mtime ) = (stat($file))[8]; utime(time, $mtime, $file); print "$time\n"; $time -= (60*60*24); unlink $file if ($mtime > $time); }#end foreach loop }#end clean_up

Replies are listed 'Best First'.
Re: How do I compare the results of my files mtime
by jasonk (Parson) on Feb 14, 2003 at 21:14 UTC

    Use the -M modification time test (perlfunc), it is already in days.

    sub clean_up { my $dir = shift; find sub { if(-M $File::Find::name > 3) { unlink($File::Find::name); } }, $dir; }
Re: How do I compare the results of my files mtime
by jacques (Priest) on Feb 14, 2003 at 21:33 UTC
    mtime does not represent the age of the file. It represents the last modify time in seconds since the epoch. For a more detailed discussion, you should see this node.