CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Get free space info from Win98 in Perl

 



brownjenkin
stranger

May 10, 2001, 7:27 AM

Post #1 of 5 (967 views)
Get free space info from Win98 in Perl Can't Post

Hello,

I'm new to Perl and need to know how to get Perl to find out how much disk space is left on any given hard disk. I have tried a few things like the following:

print `dir >dir.txt`;
open (DIR, "dir.txt");

This crashed my machine - I'm using Windows 98 SE...The purpose of the above code was to get the second last line returned when dir is invoked in DOS, ie the line that contains the amount of free disk space. Once I have that, I can join/split it into an array and use the number to perform checks, etc. I've also tried the stat command:

@stats = stat "C:/";

But when I print this information to the screen, it gives me the same numbers every time (even after deleting/adding files to C:/) ...in UNIX, you can do a stat -f filename to report stats on the file system that filename resides on - I tried adding the -f to the above code, stat -f "a file on C:/"; but it returns nothing. I've also tried piping a command to DOS which crashes the computer big time - lots of blue screens, then it just kacks out.

I should also mention that I'm using the latest (build 626) version of ActivePerl...

Does anyone have any idea why these commands are crashing my computer? Any ideas on how I can get Perl to find out how much disk space is left on any drive?

I started writing a script that checks the C: drive on a Win 98 machine for free space - when the free space gets small enough, the script looks for large files, copies them to the D: drive, compares the size of the files copied and then deletes the files on C: ...so I've completed the entire script except for the free space check. Any ideas?

I would appreciate any input.

Thanks,
Peter



freddo
User

May 10, 2001, 9:15 AM

Post #2 of 5 (960 views)
Re: Get free space info from Win98 in Perl [In reply to] Can't Post

Hello BrownJenkin,

This come from DriveInfo.pm, i just copy/pasted what was necessary to get the freespace:

Code
use Win32::API; 

sub unpack_LARGE_INTEGER ($) {
my ($b, $a) = unpack "LL", shift;
$a*2**32+$b;
}

sub GetDiskFreeSpaceEx ($) {
#==================
my $drive = shift;
return undef unless $drive =~ s/^([a-z])(:(\\)?)?$/$1:\\/i ||
$drive =~ s/^(\\\\\w+\\\w+\$?)(\\)?$/$1\\/;

$GetDiskFreeSpaceEx ||=
new Win32::API("kernel32", "GetDiskFreeSpaceEx", ['P','P','P','P'], 'N') or return;

my ($lpDirectoryName, $lpFreeBytesAvailableToCaller,
$lpTotalNumberOfBytes, $lpTotalNumberOfFreeBytes) =
($drive, "\0"x8, "\0"x8, "\0"x8);

return undef if $GetDiskFreeSpaceEx->Call(
$lpDirectoryName, $lpFreeBytesAvailableToCaller,
$lpTotalNumberOfBytes, $lpTotalNumberOfFreeBytes
) == 0;

($lpFreeBytesAvailableToCaller,
$lpTotalNumberOfBytes,
$lpTotalNumberOfFreeBytes) =
(unpack_LARGE_INTEGER ($lpFreeBytesAvailableToCaller),
unpack_LARGE_INTEGER ($lpTotalNumberOfBytes),
unpack_LARGE_INTEGER ($lpTotalNumberOfFreeBytes));

($lpFreeBytesAvailableToCaller, $lpTotalNumberOfBytes,
$lpTotalNumberOfFreeBytes);
}

@array = GetDiskFreeSpaceEx ("c");

print "Available: ", $array[0],"\n";
print "Disk Size: ", $array[1],"\n";
print "FreeSpace: ", $array[2],"\n";

I hope this helps,
Freddo

;---
Real Programmers are surprised when their cars'odometer don't turn
from 99999 to 9999A!?


brownjenkin
stranger

May 10, 2001, 10:59 AM

Post #3 of 5 (953 views)
Re: Get free space info from Win98 in Perl [In reply to] Can't Post

Okay, I got it to work!

First off, I'd like to thank Freddo for pointing me to the right module - Win32::DriveInfo ...

Here's what I did:

use Win32::DriveInfo;


($SectorsPerCluster,
$BytesPerSector,
$NumberOfFreeClusters,
$TotalNumberOfClusters,
$FreeBytesAvailableToCaller,
$TotalNumberOfBytes,
$TotalNumberOfFreeBytes) = Win32::DriveInfo::DriveSpace('f');


$TotalNumberOfFreeBytes = (Win32::DriveInfo::DriveSpace('c:\\'))[6];

print "free bytes: $TotalNumberOfFreeBytes\n";

And that returns exactly what I wanted - the *exact* amount of free space (in Bytes) on C: or whatever drive you specify.

Thanks again!
Pete



freddo
User

May 10, 2001, 11:36 AM

Post #4 of 5 (950 views)
Re: Get free space info from Win98 in Perl [In reply to] Can't Post

Hi Pete,

you're welcome Smile, happy to have helped you.


;---
Real programmers don't eat cache.


rjoseph
Novice

May 10, 2001, 12:41 PM

Post #5 of 5 (949 views)
Re: Get free space info from Win98 in Perl [In reply to] Can't Post

Very cool! I have been wondering about this myself for a while - thanks for clearing that up!!

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives