Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Getting oversize file into an array help!

by Anonymous Monk
on Oct 24, 2011 at 19:30 UTC ( [id://933466]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks!
I need to go into directories and send the files from there somewhere, my issue here is that if any file is larger than 1MB I need to report on it. My code does work if the files are under the size condition, I am stuck in capture the ones, if there is one, over the size limit. Lets say in one of these directories I will have a file size of 1.5MB or larger. I would like to add a condition and have the file in an array just like a have for the ones under the right condition. Any suggestion would be really nice! Here is the a sample code:
#!/usr/bin/perl -w use strict; ... my @directories = qw( /files1 /files2); my $min_size = 1024; my $max_size = 1048576; #1MB for my $dirs (@directories) { push @files, grep { -f && -s _ >= $min_size && -s _ <= $max_size} +read_dir( $dirs, prefix => 1 ); print "\n These files are passed the size limit: @files\n"; } ...
Thanks for looking!

Replies are listed 'Best First'.
Re: Getting oversize file into an array help!
by onelesd (Pilgrim) on Oct 24, 2011 at 20:21 UTC
    use strict ; use warnings ; my @directories = qw( /files1 /files2) ; my $min_size = 1024 ; my $max_size = 1048576 ; #1MB my @small_files ; my @big_files ; for my $dir (@directories) { for my $file (read_dir( $dir, prefix => 1 )) { next if (! -f $file || -s $file < $min_size) ; if (-s $file <= $max_size} { push(@small_files, $file) } else { push(@big_files, $file) ; } } }
      read_dir( $dir, prefix => 1 )

      On my machine, setting prefix to true doesn't prepend the path to the filename. I had to do it manually.

      Enter a line like:

      for my $file (read_dir( $dir, prefix => 1 )) { substr $file, 0, 0, "$dir/"; next if (! -f $file || -s $file < $min_size) ; ...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 09:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found