Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: Stat and file size

by tokpela (Chaplain)
on Jul 29, 2012 at 16:12 UTC ( #984297=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Stat and file size
in thread Stat and file size

I would write this a little differently to guard against a directory that has lots of files. The below example iterates over the directory handle instead of adding the directory filenames to an array. I also added more error checking to give a tighter example.

use strict; use warnings; my $testfolder = './test'; my @newfiles; if ( opendir(my $dir, $testfolder) ) { while ( my $file = readdir($dir) ) { # skip parent and current directories next if ($file eq '.' || $file eq '..'); # skip directories next if (-d $file); # create the fullpath my $full_pathname = $testfolder . "/" . $file; next unless (-e $full_pathname); # get the filesize my $filesize = -s $full_pathname; # push the non-zero sized file to the new file array if ($filesize) { push(@newfiles,$file); } } closedir($dir); } else { die "[Error] UNABLE TO OPEN DIRECTORY: [$!]"; } foreach my $file (@newfiles) { print "$file\n"; }


Comment on Re^3: Stat and file size
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://984297]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-07-13 21:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (252 votes), past polls