in reply to pushing file counts for adding

I made the below work on my web server. Will just be a few changes for you to make.

#!/usr/bin/perl -T BEGIN { $| = 1; open(STDERR, ">&STDOUT"); print "Content-type: text/plain\n\n"; } my $dirfile = "/home2/jmelpubs/public_html/jimmelanson/programming/dow +nload/directory.txt"; my $log = "/home2/jmelpubs/public_html/jimmelanson/programming/downloa +d/logfile.txt"; my $global_file_count = 0; if(open(LOGFILE, ">$log")) { if(open(DIRS, '<:encoding(UTF-8)', $dirfile)) { my @directorylist = <DIRS>; close DIRS; foreach my $dir (@directorylist) { if($dir =~ /[a-zA-Z1-9]/) { chomp($dir); if ( -e "$dir/Thumbs.db") { unlink ("$dir/Thumbs.db") or print "thumbs.db $!\n +"; } opendir(READIT, $dir); my @files = readdir(READIT); closedir(READIT); my $directory_file_count = 0; foreach my $file (@files) { unless(($file eq ".") || ($file eq "..")) { $global_file_count++ unless(-d "$_/$file"); $directory_file_count++ unless(-d "$_/$file"); } } print LOGFILE "$dir : count is $directory_file_count\n +"; } } print LOGFILE "All directories count is $global_file_count\n"; close LOGFILE; } else { die "failed to open file for reading."; } close LOGFILE; } else { die "failed to open log file for writing."; } print "Final count is: $global_file_count\n"; exit;
This is what the logfile had when it was done:
/home2/jmelpubs/public_html/jimmelanson/books : count is 7 /home2/jmelpubs/public_html/jimmelanson/downloads : count is 1 /home2/jmelpubs/public_html/jimmelanson/images : count is 18 All directories count is 26
I can't post test/script download on here. I'll try to PM that to you if you want it.

Replies are listed 'Best First'.
Re^2: pushing file counts for adding
by Anonymous Monk on Mar 21, 2016 at 23:01 UTC

    Several things:

    1. Needs a use warnings; use strict;. Always Use strict and warnings.
    2. opendir needs an or die ...;, like open.
    3. What is if($dir =~ /[a-zA-Z1-9]/) supposed to do?
    4. unless(-d "$_/$file") - which $_ is this supposed to be referring to? Shouldn't it be $dir instead of $_? (use warnings; would have told you about this one.)

      opendir doesnt NEED an or die. If you get no results, it didn't work. In this setting I'm guessing that he knows what his directories are and entered them correctly.

      The regular expression is there in case there are any blank lines in the directory.txt file, so it dosen't try to process them. But again, it would just return a zero value if it did. I nver trust user input, I try to check it where I can. Old habits. Much easier to accidently hit the return an extra return at the end of the file, this takes care of it.

      $_/$file, ya got me. I missed changing that one to $dir in the final sweep. My bad.

        opendir doesnt NEED an or die.
        $ perl -wMstrict -le 'opendir FOO, "/FOOBAR"; my @x = readdir FOO' readdir() attempted on invalid dirhandle FOO at -e line 1.

        Also, what if the directory is inaccessible, etc... debugging code that doesn't check opendir et al for errors isn't fun. (Note autodie can be useful in addition to Use strict and warnings.)

        I'm guessing that he ... entered them correctly. ... I nver trust user input, I try to check it where I can.

        ?

        The regular expression is there in case there are any blank lines in the directory.txt file

        Just that the regex will also skip files named, for example, 0_0. Probably better to just check the length.

Re^2: pushing file counts for adding
by TorontoJim (Beadle) on Mar 21, 2016 at 22:50 UTC
    Sorry, totally missed the part about pushing them to the array. In my test code you would just add push @somearray, $directory_file_count; or just use the $global_file_count as your total.