Edward G. Orton wrote:
> ----- Original Message -----
> From: <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Wednesday, February 27, 2002 12:59 PM
> Subject: Finding available space on a drive
>
>
>
>>Does anyone know any methods/modules to check the remaining
>>
> space on a
>
>>drive (Win32) that don't involve running a command such as df
>>
> or chkdsk?
>
>>Thanks,
>>
>>
> Try this:
>
> use Win32::AdminMisc;
>
> my @drives = qw ( C: D: );
> foreach my $drive (@drives) {
> my ($total, $free) = Win32::AdminMisc::GetDriveSpace($drive);
> print "Free space on drive $drive is $free\n";
> }
You should note on a reply like this that it no workey on 9x based systems.
NT-based only.
Here's one that will work on 9x also:
use strict;
use Win32::API;
my $v = 1; # set to 1 for verbose
sub DRIVE_UNKNOWN { 0; } # The drive type cannot be determined.
sub DRIVE_NO_ROOT_DIR { 1; } # The root directory does not exist.
sub DRIVE_REMOVABLE { 2; } # The drive can be removed from the drive.
sub DRIVE_FIXED { 3; } # The disk cannot be removed from the drive.
sub DRIVE_REMOTE { 4; } # The drive is a remote (network) drive.
sub DRIVE_CDROM { 5; } # The drive is a CD-ROM drive.
sub DRIVE_RAMDISK { 6; } # The drive is a RAM disk.
my $GetDriveType = new Win32::API("kernel32", "GetDriveType", ['P'], 'N') or
die Win32::FormatMessage(Win32::GetLastError());
my $GetDiskFreeSpace = new Win32::API("kernel32", "GetDiskFreeSpace",
['P','P','P','P','P'], 'N') or
die Win32::FormatMessage(Win32::GetLastError());
foreach ('A'..'Z') {
my $Drive = "$_:\\";
my $ret = $GetDriveType->Call($Drive) or
die "GetDriveType: ", Win32::FormatMessage(Win32::GetLastError());
if ($ret == DRIVE_CDROM) {
print "$_: CD-ROM\n" if $v; next;
} elsif ($ret == DRIVE_UNKNOWN) {
print "$_: Unknown\n" if $v; next;
} elsif ($ret == DRIVE_NO_ROOT_DIR) {
print "$_: No root dir\n" if $v; next;
} elsif ($ret == DRIVE_REMOVABLE) {
print "$_: Removeable\n" if $v; next;
}
$Drive = "$_:\\\0";
my $SecsPerCluster = pack ("I", 0);
my $BytesPerSec = pack ("I", 0);
my $FreeClusters = pack ("I", 0);
my $TotClusters = pack ("I", 0);
$ret = $GetDiskFreeSpace->Call($Drive, $SecsPerCluster, $BytesPerSec,
$FreeClusters, $TotClusters) or print "GetDiskFreeSpace $_: ",
Win32::FormatMessage(Win32::GetLastError());
my $SPC = unpack ("I", $SecsPerCluster);
my $BPS = unpack ("I", $BytesPerSec);
my $FC = unpack ("I", $FreeClusters);
my $TC = unpack ("I", $TotClusters);
if ($ret) {
printf "%-2.2s %-9.3f MB Free\n", $Drive, $SPC * $BPS * $FC /
(1024 * 1024);
printf "\tSecsPerCluster = %u\n\tBytesPerSec = %u\n" .
"\tFreeClusters = %u\n\tTotClusters = %u\n", $SPC, $BPS,
$FC, $TC if $v;
} else {
print "Error getting drive info '$Drive'\n";
}
}
__END__
--
,-/- __ _ _ $Bill Luebkert ICQ=14439852
(_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED]
/ ) /--< o // // http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_ Castle of Medieval Myth & Magic http://www.todbe.com/
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs