Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Error with GetDRIVE() when using a mapped drive

by vanz (Initiate)
on Nov 03, 2014 at 15:12 UTC ( #1105913=perlquestion: print w/replies, xml ) Need Help??

vanz has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I Am trying to get a map drive percentage of available space in Windows. The Drive name is Y:. The script below works when I changed the drive name to C: but when I use Y,, I receive the following error:

Use of uninitialized value in multiplication (*) at xxxx line 12. Illegal division by zero at xxxxxx line 13.

Please help and thanks in advance
#!perl use warnings; use strict; use Win32::OLE; my $fs = Win32::OLE->CreateObject('Scripting.FileSystemObject'); my $d = $fs->GetDrive('Y:'); my $TotalSize = $d->{TotalSize}; my $FreeSpace = $d->{FreeSpace}; my $usedSpace = $d->{TotalSize} -= $d->{FreeSpace}; my $AvailableSpace = $d->{AvailableSpace} *=100; my $AvailUsed = $AvailableSpace /= $d->{TotalSize}; #my $SpaceLeft = $d->{AvailableSpace} /= $AvailUsed; my $MappedDriveSpaceAvailable = $AvailUsed *= 100; print $MappedDriveSpaceAvailable;

Replies are listed 'Best First'.
Re: Error with GetDRIVE() when using a mapped drive
by Loops (Curate) on Nov 03, 2014 at 15:19 UTC

    Welcome to the monastery!,

    For whatever reason GetDrive is failing on your Y: drive. This means $d is left uninitialized and causes errors when you try to dereference it in the following lines. Change that GetDrive line to this:

    my $d = $fs->GetDrive('Y:') or die "GetDrive failed $!";
    It doesn't answer what the problem is with drive Y: on your machine.. (does it exist for sure?) but it explains what's happening in your script.
Re: Error with GetDRIVE() when using a mapped drive
by perlron (Pilgrim) on Nov 03, 2014 at 15:30 UTC
    The code is working fine on my windows 7 virtual box. I have mapped a drive to E:// and it gives me numeric output.
    For the error case, I ran your code in debug mode (perl -d and see from line 9 onwards the output of $fs->GetDrive('Y:'); is undef.
    This changes if you update the value to C:, as you pointed.
    update Is the drive you are trying to map a folder on the same machine(i used a folder on my machine so no permission / network access /privilege issues), or a network drive. If its a network drive, you might need to recheck the documentation if the invocation is right.

    The temporal difficulty with perl is u need to know C well to know the awesome.else u just keep *using* it and writing inefficient code
Re: Error with GetDRIVE() when using a mapped drive
by soonix (Canon) on Nov 04, 2014 at 08:35 UTC

    I get a similiar error when checking my CD drive when there's no CD in it, perhaps you should check the drive's isReady property.

    The below code works (for me) for fixed drives, available network shares, "ready" removable drives and gives "uninitialized" errors on "not-ready" removables - except for the DriveLetter, DriveType and isReady properties.

    (BTW if called from the command line (I use DWIM perl), I get a "drive not ready" dialogue in the latter case. When called from Padre, the "uninitialized" comes directly)

    use warnings; use strict; use Win32::OLE 'in'; my $fs = Win32::OLE->CreateObject('Scripting.FileSystemObject'); for ( in $fs->Drives ) { print "$_->{DriveLetter}:\n"; my $d = $fs->GetDrive( $_->DriveLetter ); for ( qw(DriveLetter DriveType IsReady FileSystem TotalSize FreeSpac +e AvailableSpace) ) { print "$_: $d->{$_}\n"; } }
    Note that in must be imported explicitly.
        Ah, that's probably what Padre does, then. Thanks ;)
Re: Error with GetDRIVE() when using a mapped drive
by vanz (Initiate) on Nov 03, 2014 at 16:52 UTC

    Here is the result after adding or die "GetDrive failed $!"; in the script

    Global symbol "$fs" requires explicit package name at E:\APPL\OEM\ line 9. Execution of E:\APPL\OEM\ aborted due to compilation errors.

      You have a syntax error in your edit. Likely a missing semicolon at the end of a line.

        Hi, Here is the script
        #!perl use warnings; use strict; use Win32::OLE; my $d = $fs->GetDrive('Y:') or die ; my $TotalSize = $d->{TotalSize}; my $FreeSpace = $d->{FreeSpace}; my $usedSpace = $d->{TotalSize} -= $d->{FreeSpace}; my $AvailableSpace = $d->{AvailableSpace} *=100; my $AvailUsed = $AvailableSpace /= $d->{TotalSize}; my $SpaceLeft = $d->{AvailableSpace} /= $AvailUsed; my $MappedDriveSpaceAvailable = $AvailUsed *= 100; print $MappedDriveSpaceAvailable;

        I have been looking and I do not see any syntax errors. any suggestions?

Re: Error with GetDRIVE() when using a mapped drive
by vanz (Initiate) on Nov 03, 2014 at 16:12 UTC

    Hi Guys

    I tested the or die "GetDrive failed $! options and it looped with not results.

    As for the Y drive is mapped network drive coming from another computer. What can I do to get this working ?

      What do you mean that it loops with no results? It should have died and given you an error message instead of the errors that you reported in your initial post. What was the error message that it gave you once you added the or die ... statement?

      For whatever it's worth, it works here on a network mapped drive. It may well be a permission problem you're having. The script context may not have enough privilege to access the network drive... just a guess.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1105913]
Approved by Corion
Front-paged by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2022-07-01 17:58 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (100 votes). Check out past polls.