Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Getting server share and path from a Microsoft UNC

by Anonymous Monk
on Jun 07, 2002 at 12:43 UTC ( [id://172506]=perlquestion: print w/replies, xml ) Need Help??

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

I'm sure this is an easy question, I just don't see it. how do I split the follwing path:
\\Srvbuild\e\ADW61\VENDOR\MICROSOFT\PLATFORMSDK\INCLUDE I want to split it at the first single '\' ie: \\Srvbuild\e \ADW61\VENDOR\MICROSOFT\PLATFORMSDK\INCLUDE current code: my ($network, $path) = split /^\\/, $include;

Edited by mirod 2002-06-07: changed the title (was: split) so it would not clash with a Perl keyword

Replies are listed 'Best First'.
(MeowChow) Re: split
by MeowChow (Vicar) on Jun 07, 2002 at 12:58 UTC
    You can do this manually:
      
    my $path = '\\\\Srvbuild\e\ADW61\VENDOR\MICROSOFT\PLATFORMSDK\INCLUDE' +; print join $/, $path =~ /((?:\\[^\\]*){3})(.*)/;
    Or better yet, use File::Spec, which is included with Perl:
      
    use File::Spec; my $path = '\\\\Srvbuild\e\ADW61\VENDOR\MICROSOFT\PLATFORMSDK\INCLUDE' +; print join $/, File::Spec->splitpath( $path );
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
      If you use File::Spec->splitpath, it returns server, path, and filename so you will need to have a third variable for a filename. It will also include the first directory with the server part. Not a big issues, but a couple possible gotchas.
Re: split
by c-era (Curate) on Jun 07, 2002 at 13:03 UTC
    This would be the line of code you are looking for,
    my ($network, $path) = split (/(?<!\\)\\(?!\\)/, $include, 2);
    Here is how it works:
    The regex consists of three tokens, the first token (?<!\\) is a zero width negative look behind. It makes sure that we don't have a backslash before our matching pattern, but it doesn't match it. Next we have \\ which matches a \. Finally there is (?!\\) and that does a zero width look ahead. Like the look behind, it makes sure we don't have a backslash following our pattern, but it doesn't match against it.

    The 2 at the end of the split tells split to return a max of 2 elements (network and path). If this wasn't set, then we would split on every '\' (but we wouldn't split on '\\').

Re: split
by broquaint (Abbot) on Jun 07, 2002 at 13:04 UTC
    I want to split it at the first single '\'
    Well you can split on the first '\' and you can use the 3rd argument of split to get 2 separate strings, but it won't do quite what you want, as you'll lose the '\'. If you want to split in the middle of a string then it'll be easier just to match it
    my $str = q[\\\\Srvbuild\e\ADW61\VENDOR\MICROSOFT\PLATFORMSDK\INCLUDE]; my($network, $path) = $str =~ m<^ ( (?:\\+ \w+){2} )(.*)>x; print "network - $network", $/, "path - $path", $/; __output__ network - \\Srvbuild\e path - \ADW61\VENDOR\MICROSOFT\PLATFORMSDK\INCLUDE

    HTH

    _________
    broquaint

      Thank you...I guess that was a little more difficult than I anticipated. Thanks again!
Re: split
by grinder (Bishop) on Jun 07, 2002 at 12:56 UTC
    No doubt there's a cleaner way of doing this, but my first thought is to count the number of backslashes, and then take two array slices from the split results, and join them back up again with backslashes.

    my $nr_slash = $include =~ tr/\\//; my $share = join( '\\', (split /\\/, $include)[0..3]); my $path = '\\' . join( '\\', (split /\\/, $include)[4..$nr_slash]);


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-16 11:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found