Windows Vista Forums

how can i make sure a path ends with a \

  1. #1


    Justin Rich Guest

    how can i make sure a path ends with a \

    I want to make sure a path i've pulled from a config ends with a \ and i've
    accomplished this doing this

    if($copyloc[$copyloc.length-1] -ne "\") {$copyloc += "\"}

    which works fine, im just curious if there is a better way...

    Thanks
    Justin


      My System SpecsSystem Spec

  2. #2


    Larry__Weiss Guest

    Re: how can i make sure a path ends with a \

    PowerShell does allow this equivalent syntax

    if($copyloc[-1] -ne "\") {$copyloc += "\"}

    And I'm trying hard to not use expandable strings
    unless I really intend them to be expanded, so using
    non-expandable string single quotes would get you to

    if($copyloc[-1] -ne '\') {$copyloc += '\'}

    - Larry


    On 5/12/2010 11:25 AM, Justin Rich wrote:

    > I want to make sure a path i've pulled from a config ends
    > with a \ and i've accomplished this doing this
    >
    > if($copyloc[$copyloc.length-1] -ne "\") {$copyloc += "\"}
    >
    > which works fine, im just curious if there is a better way...
    >

      My System SpecsSystem Spec

  3. #3


    PaulChavez Guest

    RE: how can i make sure a path ends with a \

    Not better, but different:

    if ($copyloc -notmatch '\\$') {$copyloc += '\'}


    "Justin Rich" wrote:

    > I want to make sure a path i've pulled from a config ends with a \ and i've
    > accomplished this doing this
    >
    > if($copyloc[$copyloc.length-1] -ne "\") {$copyloc += "\"}
    >
    > which works fine, im just curious if there is a better way...
    >
    > Thanks
    > Justin
    >
    > .
    >

      My System SpecsSystem Spec

  4. #4


    Larry__Weiss Guest

    Re: how can i make sure a path ends with a \

    And I suppose a case could be made for not having to
    repeat the character being checked for by using

    $c = '\'
    if($copyloc[-1] -ne $c) {$copyloc += $c}

    I guess I'd stop there and say that is enough refactoring.

    -------------------------------------------------

    now I go over to the "evil" side by adding ...

    -------------------------------------------------

    Just to show some alternative PowerShell "one-liner"
    fun that pulls it all together back to one line

    if($copyloc[-1] -ne ($c = '\')) {$copyloc += $c}

    and no, I don't think that last one is "better", but
    there is a "one-liner" POV that sometimes comes with
    PowerShell scripting.

    And, to me, an even worse one-liner to consider would be

    $copyloc += &{if ($copyloc[-1] -ne ($c = '\')) {$c} else {''}}

    although you do get the signal upfront that something may or
    may not get appended to $copyloc.

    - Larry


    On 5/12/2010 12:07 PM, Larry__Weiss wrote:

    > PowerShell does allow this equivalent syntax
    >
    > if($copyloc[-1] -ne "\") {$copyloc += "\"}
    >
    > And I'm trying hard to not use expandable strings
    > unless I really intend them to be expanded, so using
    > non-expandable string single quotes would get you to
    >
    > if($copyloc[-1] -ne '\') {$copyloc += '\'}
    >
    > - Larry
    >
    >
    > On 5/12/2010 11:25 AM, Justin Rich wrote:

    >> I want to make sure a path i've pulled from a config ends
    > > with a \ and i've accomplished this doing this
    >>
    >> if($copyloc[$copyloc.length-1] -ne "\") {$copyloc += "\"}
    >>
    >> which works fine, im just curious if there is a better way...
    >>

      My System SpecsSystem Spec

  5. #5


    Justin Rich Guest

    Re: how can i make sure a path ends with a \

    thanks, didnt realize using the - allowed you to work backwards, kinda cool




    "Larry__Weiss" <lfw@newsgroup> wrote in message
    news:uD6VWWf8KHA.5848@newsgroup

    > PowerShell does allow this equivalent syntax
    >
    > if($copyloc[-1] -ne "\") {$copyloc += "\"}
    >
    > And I'm trying hard to not use expandable strings
    > unless I really intend them to be expanded, so using
    > non-expandable string single quotes would get you to
    >
    > if($copyloc[-1] -ne '\') {$copyloc += '\'}
    >
    > - Larry
    >
    >
    > On 5/12/2010 11:25 AM, Justin Rich wrote:

    >> I want to make sure a path i've pulled from a config ends
    > > with a \ and i've accomplished this doing this
    >>
    >> if($copyloc[$copyloc.length-1] -ne "\") {$copyloc += "\"}
    >>
    >> which works fine, im just curious if there is a better way...
    >>

      My System SpecsSystem Spec

  6. #6


    Larry__Weiss Guest

    Re: how can i make sure a path ends with a \

    Actually I'm beginning to warm up to

    $copyloc += &{if ($copyloc[-1] -ne ($c = '\')) {$c} else {''}}

    or perhaps

    $copyloc += &{$c = '\'; if ($copyloc[-1] -ne $c) {$c} else {''}}

    as it does not leave variable $c instantiated in the
    statement scope in which it is embedded.

    And that's what spending too much time on the evil side
    will do to your mind! <grin>

    - Larry


    On 5/12/2010 12:36 PM, Larry__Weiss wrote:

    > And I suppose a case could be made for not having to
    > repeat the character being checked for by using
    >
    > $c = '\'
    > if($copyloc[-1] -ne $c) {$copyloc += $c}
    >
    > I guess I'd stop there and say that is enough refactoring.
    >
    > -------------------------------------------------
    >
    > now I go over to the "evil" side by adding ...
    >
    > -------------------------------------------------
    >
    > Just to show some alternative PowerShell "one-liner"
    > fun that pulls it all together back to one line
    >
    > if($copyloc[-1] -ne ($c = '\')) {$copyloc += $c}
    >
    > and no, I don't think that last one is "better", but
    > there is a "one-liner" POV that sometimes comes with
    > PowerShell scripting.
    >
    > And, to me, an even worse one-liner to consider would be
    >
    > $copyloc += &{if ($copyloc[-1] -ne ($c = '\')) {$c} else {''}}
    >
    > although you do get the signal upfront that something may or
    > may not get appended to $copyloc.
    >
    > - Larry
    >
    >
    > On 5/12/2010 12:07 PM, Larry__Weiss wrote:

    >> PowerShell does allow this equivalent syntax
    >>
    >> if($copyloc[-1] -ne "\") {$copyloc += "\"}
    >>
    >> And I'm trying hard to not use expandable strings
    >> unless I really intend them to be expanded, so using
    >> non-expandable string single quotes would get you to
    >>
    >> if($copyloc[-1] -ne '\') {$copyloc += '\'}
    >>
    >> - Larry
    >>
    >>
    >> On 5/12/2010 11:25 AM, Justin Rich wrote:

    >>> I want to make sure a path i've pulled from a config ends
    >> > with a \ and i've accomplished this doing this
    >>>
    >>> if($copyloc[$copyloc.length-1] -ne "\") {$copyloc += "\"}
    >>>
    >>> which works fine, im just curious if there is a better way...
    >>>

      My System SpecsSystem Spec

  7. #7


    Shay Levy [MVP] Guest

    Re: how can i make sure a path ends with a \

    Hi Justin,

    You can use the String.EndsWith() method:

    if(!$copyloc.EndsWith("\")) {$copyloc+="\"}



    ---
    Shay Levy
    Windows PowerShell MVP
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar: http://tinyurl.com/PSToolbar
    Twitter: http://twitter.com/ShayLevy



    JR> I want to make sure a path i've pulled from a config ends with a \
    JR> and i've accomplished this doing this
    JR>
    JR> if($copyloc[$copyloc.length-1] -ne "\") {$copyloc += "\"}
    JR>
    JR> which works fine, im just curious if there is a better way...
    JR>
    JR> Thanks
    JR> Justin



      My System SpecsSystem Spec

  8. #8


    Larry__Weiss Guest

    Re: how can i make sure a path ends with a \

    I like that one!
    It says exactly what is intended.

    I like to use the "-" delimited operators exclusively so
    I might code it
    if (-not $copyloc.EndsWith('\')) {$copyloc += '\'}


    One further point would be that PowerShell allows both
    of the slash characters
    \ and /
    in file paths, so it might be appropriate to check for '/'
    as well as an existing terminating character.

    Not knowing the full context of the OP's need I can't
    absolutely say whether an existing terminating '/' is
    a possibility.

    Do .NET methods allow either slash in a file path?

    - Larry


    On 5/12/2010 2:47 PM, Shay Levy [MVP] wrote:

    > Hi Justin,
    >
    > You can use the String.EndsWith() method:
    >
    > if(!$copyloc.EndsWith("\")) {$copyloc+="\"}
    >
    >
    >
    > ---
    > Shay Levy
    > Windows PowerShell MVP
    > http://blogs.microsoft.co.il/blogs/ScriptFanatic
    > PowerShell Toolbar: http://tinyurl.com/PSToolbar
    > Twitter: http://twitter.com/ShayLevy
    >
    >
    >
    > JR> I want to make sure a path i've pulled from a config ends with a \
    > JR> and i've accomplished this doing this
    > JR> JR> if($copyloc[$copyloc.length-1] -ne "\") {$copyloc += "\"}
    > JR> JR> which works fine, im just curious if there is a better way...
    > JR> JR> Thanks
    > JR> Justin
    >
    >

      My System SpecsSystem Spec

  9. #9


    Bob Landau Guest

    RE: how can i make sure a path ends with a \

    The simplest approach that I know is to add one.

    $path += '\'

    I can't hurt other than granted it looks like $#%

    Try this

    dir c:\\\windows\\system32\\\\drivers\etc\\\\hosts\\\\\

    or

    notepad c:\\\windows\\system32\\\\drivers\etc\\\\hosts\\\\\

    Window (and I believe) MS-DOS has always allowed multiple \

    "Justin Rich" wrote:

    > I want to make sure a path i've pulled from a config ends with a \ and i've
    > accomplished this doing this
    >
    > if($copyloc[$copyloc.length-1] -ne "\") {$copyloc += "\"}
    >
    > which works fine, im just curious if there is a better way...
    >
    > Thanks
    > Justin
    >
    > .
    >

      My System SpecsSystem Spec

  10. #10


    Larry__Weiss Guest

    Re: how can i make sure a path ends with a \

    And PowerShell even allows spaces

    dir "c:\windows\ \system32\ \drivers\ \etc\ \hosts"

    but not cmd.exe or notepad.exe

    -----------------------

    Curiously there is a difference in output of these two
    commands

    dir c:\\\windows\\system32\\\\drivers\etc\\\\hosts\\\\\
    dir c:\\\windows\\system32\\\\drivers\etc\\\\hosts

    Note the difference in the "Name" column

    PS C:> dir c:\\\windows\\system32\\\\drivers\etc\\\\hosts\\\\\
    Directory: C:\windows\system32\drivers\etc
    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a--- 4/14/2008 10:00 PM 734

    PS C:> dir c:\\\windows\\system32\\\\drivers\etc\\\\hosts
    Directory: C:\windows\system32\drivers\etc
    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a--- 4/14/2008 10:00 PM 734 hosts

    PS C:>

    - Larry


    On 5/12/2010 7:28 PM, Bob Landau wrote:

    > The simplest approach that I know is to add one.
    > $path += '\'
    > I can't hurt other than granted it looks like $#%
    > Try this
    > dir c:\\\windows\\system32\\\\drivers\etc\\\\hosts\\\\\
    > or
    > notepad c:\\\windows\\system32\\\\drivers\etc\\\\hosts\\\\\
    > Window (and I believe) MS-DOS has always allowed multiple \
    >
    > "Justin Rich" wrote:

    >> > I want to make sure a path i've pulled from a config ends with a \ and i've
    >> > accomplished this doing this
    >> > if($copyloc[$copyloc.length-1] -ne "\") {$copyloc += "\"}
    >> > which works fine, im just curious if there is a better way...
    >> >

      My System SpecsSystem Spec

Page 1 of 2 12 LastLast

how can i make sure a path ends with a \
Similar Threads
Thread Forum
Dcom ends with error Vista performance & maintenance
Installation ends after 1st reboot Vista installation & setup
BUG? (Test-Path $path -IsValid) and empty $path PowerShell
BUG/ANNOYANCE: PoSH autocompletes the full path rather than a minimal path PowerShell
Binding(string path) - what is the syntax for the path? Avalon