Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Disk Space used by a folder (and sub folders)

by $code or die (Deacon)
on Jan 25, 2001 at 19:02 UTC ( [id://54267]=CUFP: print w/replies, xml ) Need Help??

pass this subroutine the path (can be a UNC path!) and it will return the amount of disk space that the folder (and its subfolders) use in bytes.

I was previously using a couple of subs in a very confusing loop, but this is neater, and faster I hope.
use File::Recurse; sub GetFolderSize { my $Path = shift; my $size = 0; recurse { $size += -s } $Path; return $size; }

Replies are listed 'Best First'.
Re: Disk Space used by a folder (and sub folders)
by dws (Chancellor) on Jan 25, 2001 at 22:20 UTC
    That will give you the total size of files, but not the total space used on the filesystem for the files. If you care about that distinction, you'll need to determine what the BLOCKSIZE is on the filesystem that the files reside on, and round each file's size up to the nearest multiple of that blocksize before summing.

    As for determining blocksize.. that varies with OS. It's often 512 bytes, but can be quite a bit larger depending on how the filesystem is configured.

      That's a very good point! Thanks. I mainly use Win32, and I think that there are a few Win32 modules that will return the block size.

      $code or die
      Using perl at
      The Spiders Web
        hm, doesn't stat function exist on win32? :))
        #!/usr/bin/perl -w use strict; use File::Find; print "Total: ", GetFolderSize( defined $ARGV[0]?$ARGV[0]:'.'), $/; sub GetFolderSize { local $% = 0; find( { wanted => sub { local ($-,$=) = (-s,(stat)[11]); $%+=(int($-/$=)+($-%$=?1:0))*$= }},shift);$% }


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://54267]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-22 09:04 GMT
Find Nodes?
    Voting Booth?

    No recent polls found