Solved

find drive space.

Posted on 2001-07-02
19
929 Views
Last Modified: 2007-11-27
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
Comment
Question by:arogyarajxavier
  • 12
  • 6
19 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 6247174
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
 

Author Comment

by:arogyarajxavier
ID: 6247427
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
 
LVL 51

Accepted Solution

by:
ahoffmann earned 50 total points
ID: 6248061
how about something like:
  perl -e "@x=`dir`;print $x[$#x]"
(the fetch number of bytes with an regular expression)
0
 

Author Comment

by:arogyarajxavier
ID: 6248090
Its fine ahoffmann, how to i use this inside perl script to find free space of all available drives. thank you
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6248163
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6248165
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
 

Author Comment

by:arogyarajxavier
ID: 6251019
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
 

Author Comment

by:arogyarajxavier
ID: 6251027
without the foreach loop I put `dir c:`, I got the value "in"  in the variable $bytes.
0
 

Author Comment

by:arogyarajxavier
ID: 6251065
thanks ahoffmann,

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

the above script works fine.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:arogyarajxavier
ID: 6251083
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
 

Author Comment

by:arogyarajxavier
ID: 6251641
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6251833
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
 

Author Comment

by:arogyarajxavier
ID: 6251875
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
 

Author Comment

by:arogyarajxavier
ID: 6254661
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6255297
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
 

Author Comment

by:arogyarajxavier
ID: 6258493
Thanks ahoffmann, I couldnot find regdmp.exe file in my system. I close this question. Thanks again for ur timely help.
0
 

Author Comment

by:arogyarajxavier
ID: 6258496
Thanks ahoffmann
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6258764
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
 

Author Comment

by:arogyarajxavier
ID: 6258806
fine ahoffmann, if possible send to my yahoo mail id arogyarajxavier@yahoo.com
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now