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

I want to find out all the mapped drive letters and what they are mapped to on a PC running XP.
I have googled and searched in Monks files but I cannot see an example of Perl code that will do this.
Does any Monk have the answer?

Replies are listed 'Best First'.
Re: Finding mapped drive data
by davido (Cardinal) on May 06, 2011 at 03:19 UTC

    Googling "Finding drive info with Perl" pointed me to Win32::DriveInfo. Win32::DriveInfo::DrivesInUse() will tell you what drives exist. There are also functions to give you info on each drive, as well as to tell you what drive letters are still available.


    Dave

      Thanks for that. I did try the Perl in the link but sadly I did not have the required Win32::DriveInfo.
      However I did use your google search and found a page which explained where the drive letters are in the registry.
      The following Perl gives what I wanted.
      It works for XP but I do not know about other Windows OS
      use strict "vars"; my (%dletters, $return_code, $return_message, $jl); sub drive_letters_find($$$) { ##------------------------------------------------------------------- ## this gets the drive letters and their paths for the current PC # it returns a has where key is drive letter - value is it contents ## ------------------------------------------------------------------ my ($ref_dletters, $ref_return_code, $ref_return_message) = @_; my ($pp, $k, $key, $vals, $CurrVer, %vals); use Win32::Registry; $pp = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Map Net +work Drive MRU\\"; $$ref_return_code = $main::HKEY_CURRENT_USER->Open($pp, $CurrVer); if($$ref_return_code == 1) { $$ref_return_message = 'drive letters found'; $CurrVer->GetValues(\%vals); foreach $k (keys %vals) { $key = $vals{$k}; $ref_dletters->{$$key[0]} = $$key[2]; } } else { $$ref_return_message = "Could not open regsitry $pp\n\n"; } } drive_letters_find(\%dletters, \$return_code, \$return_message); print "\nafter drive_letters_find\nresult code <$return_code> message +<$return_message>\n"; if($return_code == 1) { print "mapped drive letters found\n"; foreach $jl (sort {$a cmp $b} keys %dletters) { print "letter <$jl> path <$dletters{$jl}>\n"; } }
Re: Finding mapped drive data
by Jenda (Abbot) on May 06, 2011 at 07:16 UTC
    use Win32::FileOp qw(Mapped); my $share = Mapped 'M:'; my %shares = Mapped();

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Re: Finding mapped drive data
by Ratazong (Monsignor) on May 06, 2011 at 07:26 UTC

    You should give the code in the following node by Marshall a try.

    It should do exactly the thing you want.

    Rata

    (and don't forget to upvote it if it serves you - it is the most undervoted note I noticed - probably due to the non-fitting title...)
Re: Finding mapped drive data
by John M. Dlugosz (Monsignor) on May 06, 2011 at 07:26 UTC
    This was also discussed a week or two ago. Maybe you can find it if you search PerlMonks.