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

Re: Stat and file size

by Ormus (Initiate)
on Jul 28, 2012 at 19:59 UTC ( [id://984229]=note: print w/replies, xml ) Need Help??


in reply to Stat and file size

I did not see how the Push link helps, sorry! How would you write it? Thanks

Replies are listed 'Best First'.
Re^2: Stat and file size
by Ormus (Initiate) on Jul 28, 2012 at 20:07 UTC
    I got it working...thanks.
    $testfolder = '.\test'; $test =~ s/\//\\/g; opendir Sdir, $testfolder; @files = readdir(Sdir); undef @newfiles; foreach $file (@files) { $full_pathname = $testfolder . "/" . $file; my ($filesize) = (stat $full_pathname)[7]; if ($filesize > 0) { $full_pathname = $testfolder . "/" . $file; if (-e $full_pathname) { @newfiles = (@newfiles,$file); } } } foreach $file (@newfiles) { print "$file\n"; }

      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"; }

      Great, Ormus!

Re^2: Stat and file size
by Anonymous Monk on Jul 28, 2012 at 20:22 UTC

    Push is used like so:

    push @array, $var1, $var2, $var3, ...;

    Therefore, @newfiles = (@newfiles,$file); should be push @newfiles, $file;

      Thanks Anonymous Monk

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-03-29 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found