in reply to using File::Find within File::Find

File::Find uses global variables (like $File::Find::name), so with these, you might encounter problems when starting two recursive instances of File::Find. It also changes the current directory of your process, which is another global state, but it restores the directory upon completion of the recursive descent. Other than that, I don't see any caveats. What happened when you tried it in your code (that you keep jealously hidden from our prying eyes)?

You likely won't find much of a speedup over your opendir, readdir and closedir solution, as File::Find uses the same functions. IO always is slow.

If you just want exactly all directories two levels down, have a look at glob:

my @directories_two_levels_down = grep { -d } glob './*/*';

Also, Yes, even you can use CPAN

Replies are listed 'Best First'.
Re^2: using File::Find within File::Find
by pauloke (Initiate) on Dec 12, 2008 at 11:00 UTC
    Apologies for not posting the code - this has been constructed of code from different forums and a little tweaking. No comments as yet.

    #!/usr/local/bin/perl -w ($#ARGV != 2) or die "Usage: $0 [directory] [rentention period in week +s]\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 ); }

    It is not complete but I was wondering if the sub ProcessDir could be more efficient.

    I will investigate glob.

    Cheers
    P

        Win32::DirSize is not available. I may just have to live with it for now unless you have any other suggestions.