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
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
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...
>
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
>
> .
>
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...
>>
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...
>>
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...
>>>
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
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
>
>
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
>
> .
>
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...
>> >
| Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dcom ends with error | glisard | Vista performance & maintenance | 1 | 10 May 2009 |
| Installation ends after 1st reboot | Paul Barkholz | Vista installation & setup | 0 | 20 Feb 2007 |
| BUG? (Test-Path $path -IsValid) and empty $path | =?Utf-8?B?Um9tYW4gS3V6bWlu?= | PowerShell | 1 | 28 Aug 2006 |
| BUG/ANNOYANCE: PoSH autocompletes the full path rather than a minimal path | Adam Milazzo [MSFT] | PowerShell | 2 | 12 Aug 2006 |
| Binding(string path) - what is the syntax for the path? | Jason Dolinger | Avalon | 2 | 10 Jan 2006 |