1.    12 May 2010 #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.    12 May 2010 #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.    12 May 2010 #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.    12 May 2010 #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.    12 May 2010 #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.    12 May 2010 #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.    12 May 2010 #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.    12 May 2010 #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.    12 May 2010 #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.    12 May 2010 #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

Similar Threads
Thread Forum
Dcom ends with error
I have a emachine computer and about 20-30 minutes after windows is started up, the Dcom stops working and says it needs to restart. I have tried to...
Vista performance & maintenance
Setting up your desktop never ends
I did a disk copy with Norton Ghost. I booted that disk. Windows comes up to the sign on screen with all users listed. I sign on to the...
Vista installation & setup
Vista on HP ends up inqueue.
I have Vista and have already been printing on my HP 4200 series printer. All of a sudden it just started going to queue and I can't retrieve them....
Vista print fax & scan
Installation ends after 1st reboot
After going through the install (clean install) for Vista Home Premium and upon first reboot all I get is a blank screen, even after I let it stay...
Vista installation & setup
BUG? (Test-Path $path -IsValid) and empty $path
I would like to check if $path is a syntactically valid path using Test-Path with -IsValid switch , i.e.: PS> $path = '' # why not? PS> if...
PowerShell
BUG/ANNOYANCE: PoSH autocompletes the full path rather than a minimal path
If you attempt tab completion on a file in a subdirectory of the current one (eg, sub<tab>\fi<tab> for subdir\file), PoSH autocompletes the full...
PowerShell
Binding(string path) - what is the syntax for the path?
Hi all, Continuing in my quest to build a custom control for a databound grid in WPF, I'm at the point where I'm trying to get each cell to...
Avalon

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • PC Help Forum
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 01:03.
    .