Re: Stat and file size
by shmem (Chancellor) on Jul 28, 2012 at 19:43 UTC
|
but when I print, it prints the files that have a file size of zero as well (prints all the files).
Of course it does print all files, since you print the elements of @files, not @newfiles.
But then, the array @newfiles would contain zero length elements, since you add $file1 to the array in a convoluted way (see push), which at that point is undefined.
Adding
use strict;
use warnings;
to the top of this code snippet's scope would catch that (and force you to declare the variables you use).
See strict, warnings | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Stat and file size
by Kenosis (Priest) on Jul 28, 2012 at 19:46 UTC
|
my @nonZeroSizeFiles = grep -s, <*>;
And in case you'd like to find zero-size files
my @zeroSizeFiles = grep !-s, <*>;
Hope this helps! | [reply] [Watch: Dir/Any] [d/l] [select] |
|
my @zeroSizeFiles = grep -z, <*>;
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Not sure how I would use that Kenosis, sorry
| [reply] [Watch: Dir/Any] |
|
use strict;
use warnings;
my $dir = './';
my @nonZeroLengthFiles = grep -s, <$dir*>;
print "$_\n" for @nonZeroLengthFiles;
This will print a list of all zero-length files in $dir. | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Stat and file size
by tobyink (Canon) on Jul 28, 2012 at 20:24 UTC
|
# Delete non-zero length-files in "/tmp".
unlink for grep -s, </tmp/*>;
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
| [reply] [Watch: Dir/Any] [d/l] |
Re: Stat and file size
by Ormus (Initiate) on Jul 28, 2012 at 19:59 UTC
|
I did not see how the Push link helps, sorry!
How would you write it? Thanks | [reply] [Watch: Dir/Any] |
|
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";
}
| [reply] [Watch: Dir/Any] [d/l] |
|
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";
}
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |