Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

hard drive space

by Anonymous Monk
on Oct 02, 2007 at 20:44 UTC ( [id://642241]=perlquestion: print w/replies, xml ) Need Help??

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

How can you calculate the size of the HD on Windows XP? I also need to know how much is used and how much free space there is.

Replies are listed 'Best First'.
Re: hard drive space
by kyle (Abbot) on Oct 02, 2007 at 21:02 UTC
Re: hard drive space
by EvanK (Chaplain) on Oct 02, 2007 at 21:47 UTC
    I think Win32::DriveInfo might work for you.
    use Win32::DriveInfo; my @attribs = Win32::DriveInfo::DriveSpace('c:');
    Update: kyle's suggested module claims to work with most *nix systems, so thats a potential plus over a Win32 module.

    __________
    Systems development is like banging your head against a wall...
    It's usually very painful, but if you're persistent, you'll get through it.

Re: hard drive space
by eyepopslikeamosquito (Archbishop) on Oct 02, 2007 at 22:10 UTC

    Using a CPAN module is the recommended way to go, as indicated by the responses above. It's also possible to solve this problem by going directly to the Win32 API, for example:

    use Win32::API; sub make_large_int { my ($lo, $hi) = @_; return $hi * (1 + 0xffffffff) + $lo; } # Return a two-element list: (size, free) in bytes. sub win_get_disk_free { my $dir = shift; # $dir is a directory on the disk of interest $dir =~ tr{/}{\\}; my $GetDiskFreeSpaceEx = Win32::API->new( 'kernel32.dll', 'GetDiskFreeSpaceEx', ['P','P','P','P'], 'I') or die; my $lpFreeBytesAvailable = pack('L2', 0, 0); my $lpTotalNumberOfBytes = pack('L2', 0, 0); my $lpTotalNumberOfFreeBytes = pack('L2', 0, 0); $GetDiskFreeSpaceEx->Call($dir, $lpFreeBytesAvailable, $lpTotalNumberOfBytes, $lpTotalNumberOfFreeBytes) or die "GetDiskFreeSpaceEx failed ($^E)\n"; return ( make_large_int(unpack('L2', $lpTotalNumberOfBytes)), make_large_int(unpack('L2', $lpTotalNumberOfFreeBytes)) ); } my ($size, $free) = win_get_disk_free('C:/'); printf "disk size = %d MB\n", $size/(1024*1024); printf "disk free = %d MB\n", $free/(1024*1024);

      Here's another Win32 specific method using windows scripting FileSystemObject:

      use warnings; use strict; use Win32::OLE; my $fso = Win32::OLE->CreateObject('Scripting.FileSystemObject'); my $drive = $fso->GetDrive('C:'); my $free = $drive->{FreeSpace}; my $size = $drive->{TotalSize}; printf "disk size = %d MB\n", $size/(1024*1024); printf "disk free = %d MB\n", $free/(1024*1024);
Re: hard drive space
by Cop (Initiate) on Oct 02, 2007 at 23:15 UTC

    or just run dir from your perl script, pipe it if you like. Extract info from output.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-24 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found