View Single Post
Old 10-21-2008   #3 (permalink)
Andy


 
 

Re: Looking for something better than split and join

On Oct 21, 9:28*am, Shay Levy [MVP] <n...@xxxxxx> wrote:
Quote:

> Hi Andy,
>
> An alternative is to use the split-path cmdlet:
>
> PS > split-path "/vol/InfraDev_AppDev03/VHD.lun" -leaf
> VHD.lun
>
> The problem is that split-path replaces each slash with a backslash in case
> of this:
>
> PS > split-path "/vol/InfraDev_AppDev03/VHD.lun" -Parent
> \vol\InfraDev_AppDev03
>
> A safer solution is a combination of substring() and lastIndexOf() methods:
>
> PS > $p.substring($p.lastIndexOf("/")+1)
> VHD.lun
>
> PS > $p.substring(0,$p.lastIndexOf("/")+1)
> /vol/InfraDev_AppDev03
>
> ---
> Shay Levy
> Windows PowerShell MVPhttp://blogs.microsoft.co.il/blogs/ScriptFanatic
> PowerShell Toolbar:http://tinyurl.com/PSToolbar
>
> A> I have a string that looks like "/vol/InfraDev_AppDev03/VHD.lun" I
> A> need to do something so I can get
> A>
> A> $vol = "/vol/InfraDev_AppDev03/"
> A> $lun = "VHD.lun"
> A> My first solution (which works but I just think its not so elegant)
> A> is to split the string on "/" and then do a join on "/" with the
> A> first
> A> two elements in the result of the split and set that to $vol, and
> A> then
> A> leave the third element as $lun.
> A> I am no regex kung fu master, but I was wondering if there was a
> A> slightly more elegant way to solve this problem.
> A>
> A> Thanks,
> A>
> A> Andy
> A>
Thanks Shay, much appreciated

Andy
My System SpecsSystem Spec