Home

Search this site:
 

Horshack’s
very
own site

 

This is Bavaria, Germany!
You will find links to Perl,
bootdisks for system administration,
a lot of Linux,
Windows administration
and some personal belongings.
Finding files older than 60 days

Finding files older than 60 days

To make long things short:
if (int(-M $filename_fqn) > 60) {
   ...
But you usually have to traverse a directory. File:Recurse from CPAN is made for this task.
#!/usr/bin/perl
use strict;
use warnings;
print "This is $0\n";
$olderthan = 60;
$finddir   = "d:/data/www/www.franken.de";
$rfiles    = &file_traverse($finddir);
@allfiles  = @{$rfiles};

# find all files older than $olderthan
foreach $file (@allfiles) {
    if ( int( -M $file ) > $olderthan ) {
        print "file is too old: $file\\n";
    }
}
print "End of $0\\n";
exit;

sub file_traverse {
    use File::Recurse;
    my (@dirs) = @_;
    my (%rules);
    my (%files);
    my (@allfiles);

    # match all
    $rules{'match'}   = '.';
    $rules{'nomatch'} = 'nothingnothingnothing';
    %files = &File::Recurse::Recurse( \@dirs, \%rules );

    # return complete filenames: "/tmp/x/y" and not "y"
    @allfiles = ();
    foreach $dir ( sort keys %files ) {
        foreach $file ( @{ $files{$dir} } ) {
            push( @allfiles, "$dir/$file" );
        }
    }
    @allfiles = sort @allfiles;
    return ( \@allfiles );
}
Impressum