in reply to Count files in directories
File::Find recurses down a directory tree from a list of starting points. When File::Find enters a directory, it does a readdir(). The output of that can be processed by a user supplied subroutine - could say prune off paths not to follow or sort the names or whatever. Here I just counted the number of simple files (-f) and returned the list of files/directories unchanged.
The wanted() subroutine will be called for each file/directory - but I don't think there is anything to be done in this example, so it does nothing.
#!/usr/bin/perl -w use strict; use File::Find; # a core module (no installation needed) # my @root= ("C:/temp"); #an array of starting directories my %options = (preprocess =>\&new_dir, wanted=>\&wanted); my $date = localtime; $date =~ s/\b(\d)\b/0$1/g; #pad single digits with zero #this seemed to be a requirement for some #reason print "$date\n"; find ( \%options, @root); sub new_dir { my @files = @_; # the readdir() output # contains both (files and directories) my $num_files = grep{-f}@files; #-f are simple files printf "%-5i %s\n", $num_files, $File::Find::dir; return @files; #return the list to continue processing } sub wanted { # not used here, but dummy sub is needed for syntax # this routine is called for each file/directory # if we wanted to do something special for each one # maybe get a size or whatever... # the full path name of this file would be in $File::Find::name } __END__ Sun Apr 01 12:18:32 2012 1278 C:/temp 8 C:/temp/dbdcsv 4 C:/temp/dictionary 2 C:/temp/inline 1 C:/temp/inline/_Inline ... blah ...
|
|---|