http://www.perlmonks.org?node_id=1025887


in reply to Windows Net Use

You can avoid splitting alltogether if you search for your share in the output of net use and then return the first word of that line.

use strict; my $inputDirectory = '"\\\\10.81.253.70\\file data\\query files"'; # need to dublicate each \ in string $inputDirectory =~ s|\\|\\\\|g; my @lines = `net use`; foreach my $line ( @lines ) { next unless $line =~ m|$inputDirectory|; $line =~ /(^\S+)/; print $1; }

This also overcomes the issue that the output of net use is sometimes split into two lines when it is too long. The output of net use sometime returns no status as well, which might be a problem or not.

Replies are listed 'Best First'.
Re^2: Windows Net Use
by Anonymous Monk on Mar 28, 2013 at 09:05 UTC

    Thnks hdb .. But when i input the directory as

    my $inputDirectory = "\\\\10.81.253.140\\IPC\$";

    It does not return anything. Why??

      You found a material gap in my code. Problem is the escaped $ at the end. The backslash in front of it will be doubly escaped and then the $ will cause trouble.

      use strict; my $inputDirectory = '\\\\10.81.253.140\\IPC$'; # need to dublicate each \ in string $inputDirectory =~ s|\\|\\\\|g; $inputDirectory =~ s|\$|\\x{24}|g; # replace $ with hex representatio +n my @lines = `net use`; foreach my $line ( @lines ) { next unless $line =~ m|$inputDirectory|; $line =~ /(^\S+)/; print $1; }

        @hdb A few more doubts.. What was the exact problem caused when we used /$(dont know ehat is this material gap..)and Also Why are we duplicating \\ to \\\\ (line 3)

      \\\\10.81.253.140\\IPC\$
      \\\\10.81.253.140\\IPC$