Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

pushing file counts for adding

by flieckster (Scribe)
on Mar 21, 2016 at 14:12 UTC ( [id://1158436]=perlquestion: print w/replies, xml ) Need Help??

flieckster has asked for the wisdom of the Perl Monks concerning the following question:

hello there, i have some code that i use to reach out to a list of directory's to count how many files are in those directory. it comes back working fine, but i wanted to change it around so that the numbers coming back from the folder counts could be added together so i can get a total, and i' can't seem to figure out how to get the total into something where i can add all the returns together. i thought that pushing the numbers coming from the counting process as it goes into each row would work, but that doesn't seem to do it.
push (@finalcount, @fileslist); $filecount = @finalcount;
#!/usr/bin/env perl use File::Find; use File::Copy; use Mail::Sender; use Net::SMTP; use File::Basename; use File::Slurp; use POSIX qw(strftime); my $date = strftime("%m-%d-%y",localtime); my $time = strftime("%I:%M:%S",localtime); chomp($myname = `id -un`); BEGIN { unshift @INC, '/usr/bin/lib' } my $findme = "/Volumes/photorepos/Perl/HAL/WorkLoad/"; my $count = "/Users/$myname/Desktop/count.txt"; my $log = "> $count"; chdir($findme) or warn "/Volumes/photorepos/Perl/HAL/WorkLoad/ $!"; #################################### #Open the file made above and the list of files to search for #################################### print "$date $time Reporting Running\n"; my $filename = 'directorys.txt'; # my $currentfiles = '/Users/flieckb/Desktop/count.txt'; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; open FILE, "$log" or warn $!; while (my $row = <$fh>) { chomp $row; foreach ($row) { chdir ($row); (@fileslist) = glob "*"; if ( -e "Thumbs.db") { unlink ("Thumbs.db") or print "thumbs.db $!\n"; } $filecount = @fileslist; if (@fileslist > 0) { # if (-d "$row"){ print FILE "$filecount\n"; push (@finalcount, @fileslist); $filecount = @finalcount; # print "total $finalcount\n"; # foreach $file (@fileslist) { # print("$file\n"); # } } # } else { # print "$row\t0\n" } } } close ; # sub us { # system "/Volumes/photorepos/Perl/HAL/WorkLoad/CurrentEmailSender.pl" +; # } # us();

Replies are listed 'Best First'.
Re: pushing file counts for adding
by Corion (Patriarch) on Mar 21, 2016 at 14:19 UTC

    So, how does your program fail to work? What output do you get, and what output do you expect and how do they differ?

    Note that glob also returns the names of directories, which may or may not be what you want.

    You should check the chdir calls for errors. Maybe some of the directories don't exist where you think they should.

      what i get now is a document on my desktop named count.txt with all the directory counted files in them. i just want to push that list of integers into another array so i can use it for calculations.

      10 28 7 5 3 1 47 7 1 10 11 14 115 2 40 9

        So, what parts of your program deal with pushing that list into another array, and how do the numbers fail to show up there? Or how are they different from what you expect?

Re: pushing file counts for adding
by TorontoJim (Beadle) on Mar 21, 2016 at 22:48 UTC

    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.

      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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1158436]
Approved by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-03-28 18:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found