http://qs1969.pair.com?node_id=270772

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

Hi Guys I am having problems with the simplest of things at the moment…

I am trying to map a drive to a remote share and I used the following code, which works fines, but how can capture the drive letter that the OS assigned, i.e the "I" letter as in the example below?
system 'net.exe use * \\\\server_1212\\d$'; Drive I: is now connected to \\ server_1212\d$. The command completed successfully.
And, instead of using net use command, is there something that is Perl native and faster that this command?... I have tried Lanman and NetResource, but they didn't work, when they did work (i.e. no error messages) I did not get a drive letter or an icon representation of the drive in "My Computer". I can't use File::Op because this requires downloading the library, and for which I don't have ftp access and even if I did have ftp access, ppm doesn't work for some reason (no matter how many times I have installed Perl and checked the path) every time I type ppm it chucks an error and with that my beloved message "Perl is not in your path"!?!? So I have to use something that is built into the Perl installation.

Many Thanks guys.

Replies are listed 'Best First'.
Re: mapping a network drive
by arthas (Hermit) on Jul 02, 2003 at 12:26 UTC

    You should grab the command output using backticks instead of system():

    my $output = `net.exe use * \\\\server_1212\\d$`; $output =~ /Drive (\w)/; my $letter = $1;

    Hope this helps!

    Michele.

Re: mapping a network drive
by fglock (Vicar) on Jul 02, 2003 at 12:26 UTC

    You can assign a drive name yourself:

    system 'net.exe use I: \\\\server_1212\\d$';

    Or, use backticks to capture the command output.

    @result = `net.exe use * \\\\server_1212\\d$`;

      The latter is probably a better choice, as it won't fail if a network path is alreadi assigned to the letter I.

      Michele.

      Ahh, the back ticks instead of the system, oh brother.....damn. Thanks very much...god bless.
      this is the reason why I was using net use *,...i wanted any available drive, the I drive could be mapped to somewhere else on another machine….Anyway, I thought there might be more elegant way to grab the dive letter assigned rather than having to write a pattern matching code. Thanks for the tip.
Re: mapping a network drive
by fglock (Vicar) on Jul 02, 2003 at 18:12 UTC

    You should also test if the drive is already mapped, or you risk mapping yet another drive letter I:, J:, K:, ... each time you run the script :)