• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

how can i make sure a path ends with a \

J

Justin Rich

#1
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
 
L

Larry__Weiss

#2
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...
>
 
P

PaulChavez

#3
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
>
> .
>
 
L

Larry__Weiss

#4
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...
>>
 
J

Justin Rich

#5
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...
>>
 
L

Larry__Weiss

#6
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...
>>>
 
S

Shay Levy [MVP]

#7
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
 
L

Larry__Weiss

#8
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
>
>
 
B

Bob Landau

#9
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
>
> .
>
 
L

Larry__Weiss

#10
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...
>> >
 
S

Shay Levy [MVP]

#11
I believe they do allow both slash types:

PS > [io.path]::DirectorySeparatorChar
\

PS > [io.path]::AltDirectorySeparatorChar
/


$f = New-Object System.IO.FileInfo C:/foo.txt
$f.copyto("d:/scripts/temp/foo.bar")



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



L> Thanks for the additional info.
L>
L> Do you know the answer to my other question?
L>
L> Do .NET methods allow either slash in a file path?
L>
L> - Larry
L>
L> On 5/12/2010 5:05 PM, Shay Levy [MVP] wrote:
L>

>> No it will not match '/'. Use regex to compare against '/' and '\'
>> use a
>> regex:
>> "string" -match '(\\|/)$'
>> Note that EndsWith() is case-sensitive. There's an overload we can
>> use to ignore casing:
>>
>> PS > "shay".endswith("Y","OrdinalIgnoreCase")
>> True
>> And there's another overload for culture aware comparisons
>> (http://msdn.microsoft.com/en-us/library/83xftk75(v=VS.100).aspx)
>> .
>>
>> Finally, there's also a StartsWith() method.
>>
>> ---
>> Shay Levy
>> Windows PowerShell MVP
>> http://blogs.microsoft.co.il/blogs/ScriptFanatic
>> PowerShell Toolbar: http://tinyurl.com/PSToolbar
>> Twitter: http://twitter.com/ShayLevy
>> L> I like that one!
>> L> It says exactly what is intended.
>> L> I like to use the "-" delimited operators exclusively so
>> L> I might code it
>> L> if (-not $copyloc.EndsWith('\')) {$copyloc += '\'}
>> L> One further point would be that PowerShell allows both
>> L> of the slash characters
>> L> \ and /
>> L> in file paths, so it might be appropriate to check for '/'
>> L> as well as an existing terminating character.
>> L> L> Not knowing the full context of the OP's need I can't
>> absolutely say
>> L> whether an existing terminating '/' is a possibility.
>> L> L> Do .NET methods allow either slash in a file path?
>> L> L> - Larry
>> L> L> On 5/12/2010 2:47 PM, Shay Levy [MVP] wrote:
>> L>

>>>> 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