Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting file size

Status
Not open for further replies.

oinky

Programmer
Oct 29, 2001
45
US
I am having some trouble getting the file size operator to work right. I have something like:
--------------------------------------
$myfolder = "c:/test";
opendir(DIR, $myfolder) or die "cant open $myfolder $!";
while (defined ($filename = readdir(DIR)))
{
$sum = -s $filename;
print "sum: $sum file: $filename\n";
}

---------------------------------------
It gets the file names correctly, however, it seems that
it cannot return the size of the file. I was pretty sure that "-s $filename" would work, but it returns nothing.
If any1 have an idea whats going on here please help. Thanks.
 
Since you didn't actually change to the directory you are reading, the -s is looking for $filename in the CURRENT directory, not the one you are reading. Try this:
Code:
$sum = -s $myfolder.'/'.$filename;
What are you indicating by calling the variable $sum? If you want to accumulate the total size, you need += instead of just =, but then you wouldn't want to display it for each file.

ALSO - you should have some code in there to omit the . and .. directory entries. Something like this:
Code:
   next if $filename =~ /^\.{1,2}$/;

OR
   next if $filename eq '.';
   next if $filename eq '..';
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
cool that worked. Thanks a lot. 8)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top