in reply to perl robocopy to temp mapped drive.

If I understand your question, you are trying to find a drive letter you can pass to "net use" to map.

My suggestion is that you let "net use" figure that out, ane let you know what it mapped.

The syntax "net use * \\UNC\PATH" will pick an available drive letter.

You can scan the output of that command to discover what drive it used. eg:

>net use * \\myserver\pub\MyPath Drive Y: is now connected to \\myserver\pub\MyPath. The command completed successfully.

                Memory fault   --   brain fried

Replies are listed 'Best First'.
Re^2: perl robocopy to temp mapped drive.
by 3dbc (Monk) on Oct 11, 2018 at 19:52 UTC
    This does it, thanks for the help Perl Monks!
    # $destination is the full unc path to the share directory # $extract is the full path the the individual file to be copied my $drive = netUse ($destination); copyDat ($extract, $drive); sub netUse { my @net_use_output; my $output; chomp $_[0]; $_ = $1 if($_[0]=~/(.*)\\$/); print "\nnet use \* \"$_[0]\""; #my $output = `net use \* \"$_[0]\" 2>&1|`; open TASK, "net use \* \"$_[0]\" 2>&1|" or die "cannot map $_[0]" +; while (<TASK>) { print "\n"; print; #chomp; #chop; push (@net_use_output, $_); #return $_; } print join(", ", @net_use_output); print "\n"; print "\n"; print "-----\n"; print @net_use_output[0]; $output = $2 if(@net_use_output[0]=~/^(Drive\s)([A-Z])\:(.*)$/); print "\n"; print "\n"; print "-----\n"; print $output; return $output; } sub copyDat { my $file = basename($_[0]); my $path = dirname($_[0]); #$path .= "\\"; #This one's for James print "\n File: $file \n\n"; print "\n Dirname: $path \n\n"; #chomp $_[1]; #my $map = $_[1]; #my $map = $1 if($map=~/(.*)\\$/); print "\nROBOCOPY \"$path\" $_[1]\:\\ $file /B \n\n"; open TASK, "ROBOCOPY \"$path\" $_[1]\:\\ $file /B 2>&1|" or die "c +annot $!"; while (<TASK>) { print; #return $_; } print "\nnet use $_[1]\:\\ /delete \n\n"; open TASK2, "net use $_[1]\: /delete 2>&1|" or die "cannot $!"; while (<TASK2>) { print; #return $_; } }
    - 3dbc
Re^2: perl robocopy to temp mapped drive.
by 3dbc (Monk) on Oct 11, 2018 at 15:48 UTC
    I attempted to run net use * and pull the output but am getting errors.

    #!/usr/bin/perl -w use strict; use warnings; my $destination = qq(\\\\BLAH\\NESTED\\OBFUSCATED\.WITH SPECIAL_CHARS\ +\SHARE\\NAME\\DOWN\\DEEP); netUse ($destination); sub netUse { my @net_use_output; chomp $_[0]; $_ = $1 if($_[0]=~/(.*)\\$/); print "\nnet use \* \"$_[0]\""; open TASK, "net use \* \"$_[0]\"" or die "cannot map $_[0]"; while (<TASK>) { print; #chomp; #chop; #push (@net_use_output, $_); #return $_; } #print join(", ", @net_use_output); #return @net_use_output; }
    above code always produces error:
    cannot map \\BLAH\NESTED\OBFUSCATED.WITH SPECIAL_CHARS\SHARE\NAME\DOWN +\DEEP at ..\ examples\blah.pl line 12, <TASK> line 1. net use * "\\BLAH\NESTED\OBFUSCATED.WITH SPECIAL_CHARS\SHARE\NAME\DOWN +\DEEP"
    Even though copying and pasting the same command on the cmd shell works perfectly?

    - 3dbc

      Try adding pipe to open

          open TASK, '-|',"net use \* \"$_[0]\"" or die "cannot map $_[0]";

      or alternatively

      #!perl use strict; use warnings; use IPC::System::Simple qw/capture/; my $destination = "\\\\BLAH\\NESTED\\OBFUSCATED\.WITH SPECIAL_CHARS\\S +HARE\\NAME\\DOWN\\DEEP"; print netUse($destination); sub netUse { my $path = shift; my $cmd = "net use * $path"; my ($msg) = grep /Drive/, capture($cmd); return ($msg =~ /Drive (.*) is now connected/); }
      poj
        I needed to add the 2>&1| guess that's the pipe you're talking about. Thanks.
        sub netUse { my @net_use_output; my $output; chomp $_[0]; $_ = $1 if($_[0]=~/(.*)\\$/); print "\nnet use \* \"$_[0]\""; #my $output = `net use \* \"$_[0]\" 2>&1|`; open TASK, "net use \* \"$_[0]\" 2>&1|" or die "cannot map $_[0]" +; while (<TASK>) { print "\n"; print; #chomp; #chop; push (@net_use_output, $_); #return $_; } print join(", ", @net_use_output); print "\n"; print "\n"; print "-----\n"; print @net_use_output[0]; $output = $2 if(@net_use_output[0]=~/^(Drive\s)([A-Z])\:(.*)$/); print "\n"; print "\n"; print "-----\n"; print $output; #return @net_use_output; }
        Returns:
        net use * "\\BLAH\NESTED\OBFUSCATED.WITH SPECIAL_CHARS\SHARE\NAME\DOWN +\DEEP" Drive S: is now connected to \\BLAH\NESTED\OBFUSCATED.WITH SPECIAL_CHA +RS\SHARE\NAME\DOWN\DEEP. The command completed successfully. Drive S: is now connected to \\BLAH\NESTED\OBFUSCATED.WITH SPECIAL_CHA +RS\SHARE\NAME\DOWN\DEEP. , , The command completed successfully. , ----- Drive S: is now connected to \\BLAH\NESTED\OBFUSCATED.WITH SPECIAL_CHA +RS\SHARE\NAME\DOWN\DEEP. ----- S
        - 3dbc