Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: hard drive space

by eyepopslikeamosquito (Archbishop)
on Oct 02, 2007 at 22:10 UTC ( [id://642253]=note: print w/replies, xml ) Need Help??


in reply to hard drive space

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);

Replies are listed 'Best First'.
Re^2: hard drive space
by bmann (Priest) on Oct 03, 2007 at 00:57 UTC
    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);

Log In?
Username:
Password:

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

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

    No recent polls found