header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Trim String Left to Right based on Pattern
Last Post 13 Aug 2010 06:27 AM by Shay Levy. 6 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
jcridgeUser is Offline
New Member
New Member
Posts:4
Avatar

--
12 Aug 2010 08:15 AM
    I've searched for a simple solution to this, but haven't been able to find one yet. 

    I've got an array of strings of directory paths.  They are all different and all different lengths.  I only want to retain the last parent and subdirectory in each path.  For example:

    \program files\adobe\working\documents\bob
    \users\bob\documents\engineering\poc
    \source code\professional services\GM\2010\august\10\development\perl
     

    I only want "documents\bob", "engineering\poc" and "development\perl". 

    I've tried using different variations of string split("\") and trim, but have not had any luck.  Is there an easy way to do this in PowerShell?

    Thanks,
    jcridge
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    12 Aug 2010 08:37 AM
    "\program files\adobe\working\documents\bob","\users\bob\documents\engineering\poc","\source code\professional services\GM\2010\august\10\development\perl" | foreach {
    $_.split("\\")[-2,-1] -join "\"
    }

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    jcridgeUser is Offline
    New Member
    New Member
    Posts:4
    Avatar

    --
    12 Aug 2010 09:20 AM
    That is perfect.  Works like a charm.  I still have a lot to learn and didn't realize I could use "-" (negative, from the left) with split.

    Thank you very much. 

    Is there a good PowerShell command reference guide you can suggest?

    Thanks,
    jcridge
    BustedFlushUser is Offline
    New Member
    New Member
    Posts:97
    Avatar

    --
    12 Aug 2010 01:37 PM
    Shay, do you mind explaining what you did just there? I'm trying to follow along, but I'm very confused.

    I can't figure out why the delimiter is "\\"
    I don't know what the index values [-2,-1] are doing, nor the -join.

    Thanks!
    Scott HendricksUser is Offline
    New Member
    New Member
    Posts:12
    Avatar

    --
    12 Aug 2010 01:58 PM
    The split works with just the single backslash:
    PS C:\> $("foo\bar").Split("\")
    foo
    bar
    I'm guessing the double backslash was unintentional. The C# syntax uses the double backslash because in C# backslash is the escape character. I do it all the time.

    The negative numbers in the notation are like an index from the end of the array. [-1] returns the last element, [-2] returns the second to last, etc.

    The -Join operator is a very nice feature when dealing with arrays of strings. You can concatenate all the strings in an array, where the separator between each array element is what is passed to the join operation. For example:
    PS C:\> @("Foo","Bar") -Join "-"
    Foo-Bar
    
    PS C:\> @("Foo","Bar","Hi") -Join "-"
    Foo-Bar-Hi
    BustedFlushUser is Offline
    New Member
    New Member
    Posts:97
    Avatar

    --
    13 Aug 2010 04:15 AM
    Ah ha, good stuff, thanks!
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    13 Aug 2010 06:27 AM
    Yup, the Split method just one works just fine one backslash, it was a leftover from previous testings with the Split operator which requires double backslash.

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer