You are doing a lot of work that can be done with the core File::Find module.

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 ...

In reply to Re: Count files in directories by Marshall
in thread Count files in directories by begood321

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.