Reputation: 1438
I am trying to find the size of a file using the -s
operator. It looks like this:
my $filesz = -s $filename
I tried lots of various way, but it can not get this size.
However, if I give static content instead of filename, it works fine
For example:
$filesz = -s "/tmp/abc.txt"
This works fine.
I tried adding "
in the filename, it didn't work. I removed \n
from filename using chomp
, but the problem remains the same. What's wrong here?
Upvotes: 7
Views: 26480
Reputation: 1
i think you should try this your getting from a readdir:
#!/usr/bin/perl -w
use strict;
use warnings;
my $filedir = "/documents/sample/file.txt";
opendir(my $dir, $filedir) or die "Could not load directory";
my @read_dir = sort grep {!-d}readdir($dir);
foreach my $fileInDir(@read_dir)
{
my $currentDestination = "$filedir/$fileInDir";
my $filesize = -s $currentDestination;
if(-z $currentDestination)
{
print "Zero - Filename: $fileInDir Size: $filesize\n";
}
else
{
print "Non - Zero Filename: $fileInDir Size: $filesize\n";
}
}
Upvotes: 0
Reputation: 69264
As hobbs says, the most likely explanation is that $filename
doesn't contain what you think it does.
Based on previous experience, I'd go further than that and hesitate a guess that $filename
has a newline character at the end of it. Are you reading the value in $filename
from a file or from user input?
Upvotes: 1
Reputation: 239890
-s $filename
works just fine; the only conclusion is that there's no file with the name contained in $filename
. Take a very close look at the contents of $filename
, and make sure that your working directory is what you think it is.
Upvotes: 17