Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl-Sensitive Sunglasses

Disk Space used by a folder (and sub folders)

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

Description: 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;
Comment on Disk Space used by a folder (and sub folders)
Download Code
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);$% }


Back to Snippets Section

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2014-03-17 10:57 GMT
Find Nodes?
    Voting Booth?

    Have you used a cryptocurrency?

    Results (346 votes), past polls