#!/usr/local/bin/perl -w ($#ARGV != 2) or die "Usage: $0 [directory] [rentention period in weeks]\n"; use strict; use warnings; use File::Find; use File::stat; use Time::localtime; use Time::Local; my ($dir_path, $ret) = @ARGV; my $rdate = time - ($ret * 24 * 60 * 60); my $dir_depth = 1; my %size; my %date; find( { preprocess => \&preprocess, wanted => \&wanted,} , $dir_path); my @sorted = sort {$size{$b} <=> $size{$a}} keys %size; print "\nList of directories older than: ".ctime($rdate)."\n\n"; printf "%-10s %-24s %s", "Size", "Modified Date", "Directory Path\n\n"; foreach (@sorted) { printf "%10d %s %s\n", $size{$_}, ctime($date{$_}), $_; } sub preprocess { my $depth = $File::Find::dir =~ tr[/][]; return @_ if $depth < $dir_depth; return grep { not -d } @_ if $depth == $dir_depth; return; } sub wanted { my $depth = $File::Find::dir =~ tr[/][]; my $mod_date = (stat($File::Find::name)->mtime); if ( ($mod_date < $rdate) && ($depth == $dir_depth)) { $size{$File::Find::name} = ProcessDir($File::Find::name) if -d; $date{$File::Find::name} = $mod_date if -d; } return if $depth == $dir_depth; } sub ProcessDir { my( $Path ) = @_; my @DirList; my $DirSize = 0; if( opendir( DIR, $Path ) ) { while( my $Object = readdir( DIR ) ) { next if( "." eq $Object || ".." eq $Object ); my $ObjectPath = "$Path\\$Object"; if( -d $ObjectPath ) { push( @DirList, $ObjectPath ) } else { $DirSize += -s $ObjectPath; } } closedir( DIR ); } foreach my $ObjectPath ( @DirList ) { $DirSize += ProcessDir( $ObjectPath ); } return( $DirSize ); }