http://qs1969.pair.com?node_id=54267

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);$% }

        --
        AltBlue.