use strict; use warnings; use diagnostics; use Cwd; # module for finding the current working directory my @root = ( ### Workareas ### #"Y:\\default\\main\\test.com\\WORKAREA\\www\\managed-assets\\www\\sma +rtphones\\test-one-v\\all-round-view", "C:\\Users\\Ben\\Documents\\test\\", #"U:\\default\\main\\WORKAREA\\", ); ### just to make sure the file is emptied before we start my $output_file = qq|C:\\Users\\Ben\\Documents\\test\\tools\\count_res +ults.txt|; my $output_file2 = qq|C:\\Users\\Ben\\Documents\\test\\tools\\dirs_sea +rched.txt|; open(MYFILE, ">$output_file") || die "opening $output_file: $!"; # ov +erwrite my $dir = ""; my $cmd = "C:\\Perl\\bin\\perl -le \"print scalar localtime\""; my $date = `$cmd`; my $count; my @final_count; my @files; foreach(@root) { &write_file("$date"); print "\nScanning : $_\n\n"; &ScanDirectory($_); } close(MYFILE); ############################################### # ScanDirectory ############################################### sub ScanDirectory { my ($path) = shift; my($startdir) = &cwd; # keep track of where we began chdir($path) or die "Unable to enter dir $path:$!\n"; opendir(DIR, ".") or die "Unable to open $path:$!\n"; my @names = readdir(DIR); closedir(DIR); my $count = 0; # LOOP RECURSIVELY foreach my $name (@names){ ### skipped next if ($name eq "."); next if ($name eq ".."); #next if ($name =~ m/.*\.\w+/); ### Skip symlink #next if(-l $name); ### Check to see if directory if (-d $name){ next if($name !~ m|(^[a-zA-Z0-9])|); print qq|\nScanning dir $startdir/$path/$name ...|; @files = <$startdir/$path/$name/*>; $count = @files; &write_file2("Scanning dir $startdir/$path/$name ...\n"); &ScanDirectory($name); next; } } print qq|\nTotal file count: $count|; chdir($startdir) or die "Unable to change to dir $startdir:$!\n"; } ###################################################################### +### # padZero pads single digit dates and time with a leading zero. ###################################################################### +### sub write_file { my $str = shift; open(MYFILE, ">>$output_file") || die "opening $output_file: $!"; + # append print MYFILE $str; close(MYFILE); } # end write_file sub write_file2 { my $str2 = shift; open(MYFILE2, ">>$output_file2") || die "opening $output_file2: $! +"; # append print MYFILE2 $str2; close(MYFILE2); } # end write_file

Not counting all files in directories. It appears glob has limitations to some files

@files = <$startdir/$path/$name/*>; $count = @files;

I'm trying to see if I can find solution to get all files to be counted

I also tried another foreach loop which will work for 1 directory but have difficulty printing dir then count in next line as it does now recursively.


In reply to 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.