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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |