Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 978
  • Last Modified:

find drive space.

I am using WindowNT 4, Perl 5.003_07 (Larry Wall). I want to find free space of the drives in a machine. help me
0
arogyarajxavier
Asked:
arogyarajxavier
  • 12
  • 6
1 Solution
 
Kim RyanIT ConsultantCommented:
There is a module you will need to first download and install. All the info is at
http://search.cpan.org/search?dist=Win32-DriveInfo
Then it is as simple as using code like this

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

0
 
arogyarajxavierAuthor Commented:
thank you teraplane. It clearly says Perl 5.004 needed to use the Win32::DriveInfo. But I am using Perl 5.003_07, I could not upgrade right now. So please suggest me other ways to find drive information.
0
 
ahoffmannCommented:
how about something like:
  perl -e "@x=`dir`;print $x[$#x]"
(the fetch number of bytes with an regular expression)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
arogyarajxavierAuthor Commented:
Its fine ahoffmann, how to i use this inside perl script to find free space of all available drives. thank you
0
 
ahoffmannCommented:
foreach $d ('c:' 'd:') {
  $lastline=`dir`;
  ($dumm1,$dumm2,$bytes,@rest)=split(/\s+/,$lastline);
  $bytes=~s/\.\,//;
}
# since perl 5.001 backticks should work even under M$
0
 
ahoffmannCommented:
foreach $d ('c:' 'd:') {
  $lastline=`dir $d`;
  ($dumm1,$dumm2,$bytes,@rest)=split(/\s+/,$lastline);
  $bytes=~s/\.\,//;
}
# since perl 5.001 backticks should work even under M$
0
 
arogyarajxavierAuthor Commented:
thanks ahoffmann,
I got the following error message:
String found where operator expected at freespace.pl line 1, near "'c:' 'd:'"
        (Missing operator before  'd:'?)
syntax error at freespace.pl line 1, near "'c:' 'd:'"
syntax error at freespace.pl line 6, near "}"
Execution of freespace.pl aborted due to compilation errors.
0
 
arogyarajxavierAuthor Commented:
without the foreach loop I put `dir c:`, I got the value "in"  in the variable $bytes.
0
 
arogyarajxavierAuthor Commented:
thanks ahoffmann,

foreach $drive ('c:','d:') {
  @lastline=`dir $drive`;
  print $lastline[$#lastline];
}

the above script works fine.
0
 
arogyarajxavierAuthor Commented:
how to I get the total space?. My aim is to find the percentage of available free space. I am getting the free space value in string format. How to get both total and available free space in numeric format. thanks.
0
 
arogyarajxavierAuthor Commented:
The following script find the numeric value of the available free space. Please help me to find the total space of the dirve. thanks

foreach $drive ('c: /-c','d:/-c') {
@lastline=`dir $drive`;
$x=$lastline[$#lastline];
($b,$freespace)=split(/\s+/,$x);
$totalfreespace=int($freespace);
print "$totalfreespace \n";
}

0
 
ahoffmannCommented:
I don't know of a tool giving the total size of a drive.
You may try to use fdisk, but I'm not shure if you can pipe the required commands into it.
Another solution might be using   dir /c/w  and piping the output to script (awk) which counts the used bytes.
'Cause NT's explorer.exe can show the disksize, there is probably an option to get it from command line too.
0
 
arogyarajxavierAuthor Commented:
thanks ahoffmann,
The command dir/s produce the following two lines at the end.

  34471 File(s)  1,041,144,676 bytes
                 6,707,159,040 bytes free

using
$x=$lastline[$#lastline];

I can get the 'bytes free'. how to i extract the previous line.?


0
 
arogyarajxavierAuthor Commented:
thanks ahoffmann.

The following script solved my problem, It will produce
approximate total space of the drives.

foreach $drive ('c:\ /-c/ar/ah/as/aa/s','d:\ /-c/ar/ah/as/aa/s')
{
     @lastline=`dir $drive`;
     $x=$lastline[$#lastline];
     $y=$lastline[$#lastline-1];
     ($aa,$bb,$dd,$usedspace,$cc)=split(/\s+/,$y);
     ($b,$freespace)=split(/\s+/,$x);
     $totalfreespace =int($freespace);
     $totalSpace=int($usedspace) + $totalfreespace;
}

Only one question: is there any way to find the available drives in a machine using perl script? give ur suggestion, i will close this question immediately. thanks again.
0
 
ahoffmannCommented:
you need to love NT :-]]

use regdmp.exe (from resource kit) to generate an ASCII dump of the registries LOCAL_MACHINE hive, like:
   regdmp HKEY_LOCAL_MACHINE>hklm.reg
Then you can use perl to find the drives like:
   perl -nae "if (m/^\s+\\DosDevices/){
        $d=$F[0];$d=~s/.*\\//; print \"$d\n\";}"

Feel free to extract those drives you're interested in.
0
 
arogyarajxavierAuthor Commented:
Thanks ahoffmann, I couldnot find regdmp.exe file in my system. I close this question. Thanks again for ur timely help.
0
 
arogyarajxavierAuthor Commented:
Thanks ahoffmann
0
 
ahoffmannCommented:
regdmp.exe as regini.exe etc. is part of M$ NT Resource Kit, offered on a special CD. Not shure if M$ offers it on there web site too. If you can't find it in the net anyhow, I can send it.
0
 
arogyarajxavierAuthor Commented:
fine ahoffmann, if possible send to my yahoo mail id arogyarajxavier@yahoo.com
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 12
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now