Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

file size help

by Anonymous Monk
on Jun 17, 2003 at 19:27 UTC ( [id://266611]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello perl friends , I want to know if there is a way through perl to find out the size of a file in kilobytes thank you friends

Replies are listed 'Best First'.
Re: file size help
by crouchingpenguin (Priest) on Jun 17, 2003 at 19:33 UTC
    my $file_name = 'file_name.png'; my $size_in_bytes = -s $file_name;

    cp
    ----
    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
Re: file size help
by arthas (Hermit) on Jun 17, 2003 at 19:38 UTC

    This should do what you need:

    $bytes = (stat('test.jpg'))[7]; $kbytes = int($bytes/1024);

    As an alternative, you can use -s insstead of stat():

    $bytes = -s 'test.jpg';

    Michele.

      One note which I would add to this, is that both of these functions employ the stat() function "under the hood" - Information returned from this call is cached such that subsequent calls to stat, lstat or the -X file tests employing the special filehandle consisting of a solitary underline character (_) can employ this cached information rather than again calling the underlying stat() function.

      This behaviour is documented in perlfunc.

       

      perl -le 'print+unpack"N",pack"B32","00000000000000000000001001101011"'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 03:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found