Home: Perl Programming Help: Beginner:
Filesize



scuffed
stranger

May 8, 2001, 10:56 AM


Views: 495
Filesize

ok i am trying to show the filesize on something i am making i ripped this script off of fileman which shows the file sizes
sub fs {
# --------------------------------------------------------
# Prints out the file size.

my $size = shift;
my $formatted_size = int($size / 1024) . " kb";
$formatted_size == 0 ?
return "$size bytes" :
return $formatted_size;
}
and i made all the variables along with the filesize and crap but it still wouldn't show the file size of the file i needed. can anyone help me out with this please?



rjoseph
Novice

May 8, 2001, 12:46 PM


Views: 492
Re: Filesize

It seems that something is broken in the way you get the size of the file in the first place (atleast everything else seems like it would work). The -s modifier will return the size of a file, or you could use the stat(). Here is how I would do it though:

Code
my $size = -s 'my_file.txt'; 
my $kb_s = int($size / 1024);

Of course, I would use sprintf to round because you can specify decimal places to round to, and it is more accurate.

Hope that helps!

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


scuffed
stranger

May 8, 2001, 9:09 PM


Views: 485
Re: Filesize

Well thank you i am basically new at this stuff right now and i appreaciate it thank you.