Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Manipulating current directory as a variable

Reply
 
Old 08-27-2006   #1 (permalink)
=?Utf-8?B?UmFsaXNo?=


 
 

Manipulating current directory as a variable

I feel extremely stupid for asking this:

How does one set a variable to the current directory and manipulate it? This
apparently can't be done with $test = pwd as on trying to manipulate it, e.g.
$test = $test + "\test" an error is received:

Method invocation failed because [System.Management.Automation.PathInfo]
doesn't contain a method n
amed 'op_Addition'.
At line:1 char:16
+ $test = $test + <<<< "\test"

Thanks in advance,

Ralish



My System SpecsSystem Spec
Old 08-27-2006   #2 (permalink)
Jouko Kynsijärvi


 
 

Re: Manipulating current directory as a variable

Ralish wrote:
> How does one set a variable to the current directory and manipulate
> it? This apparently can't be done with $test = pwd as on trying to
> manipulate it, e.g. $test = $test + "\test" an error is received:
>
> Method invocation failed because
> [System.Management.Automation.PathInfo] doesn't contain a method n
> amed 'op_Addition'.
> At line:1 char:16
> + $test = $test + <<<< "\test"


Since pwd (alias for get-location) doesn't return a string, you can't
concatenate to it, but use this:

$test = (pwd).path
$test = $test + "\test"

Or just use the join-path cmdlet:

join-path (pwd) test


My System SpecsSystem Spec
Old 08-27-2006   #3 (permalink)
=?Utf-8?B?U3VuZyBNIEtpbQ==?=


 
 

RE: Manipulating current directory as a variable


"Ralish" wrote:

> How does one set a variable to the current directory and manipulate it?


There are many cmdlets related to manipulating "path"
If you type the following you will see about half a dozen commands
PS> Get-Command -Noun Path

> This apparently can't be done with $test = pwd as on trying to manipulate it, e.g.
> $test = $test + "\test" an error is received:


What happening here is that, when you do "$test = pwd", "$test" variable
actually holds an object of type "System.Management.Automation.PathInfo" not
a text path one might expect. So you have to actually get the path string
from "pwd".(by the way, to see what methods and properties are avaiable for
"pwd" or ANY other objects, you can use "Get-Member".
Try out the following command and you will see a property named "Path" which
is the actually string holding the directory path of the current directory.
PS> pwd | gm)
PS> $test = (pwd).Path

In the above code, I have surrounded "pwd" with "()" and then retrieved the
object's "Path" value and stored it into "$test" variable.

> Method invocation failed because [System.Management.Automation.PathInfo]
> doesn't contain a method n
> amed 'op_Addition'.
> At line:1 char:16
> + $test = $test + <<<< "\test"


As you saw in "pwd | gm", object of type
"System.Management.Automation.PathInfo" does not override "+"(which is
represented by "op_Addition") operator so that is the reason.

So to be able to accomplish appending "\test" to $test variable, there are
couple of ways to do this(or more perhaps )

==============================
# This produces an errorenous path
[^_^]PS[5]>$test = (pwd).path
[^_^]PS[6]>$test += "\test"
[^_^]PS[7]>$test
C:\\test
==============================
# It is safer to use built-in "Join-Path" cmdlet
# The current directory is "C:\"
[^_^]PS[841]>(pwd).path
C:\
# Assign the string path to $test then join the path "c:\" with "test"
# One thing to note that, "Join-Path" actually disregards double "\"
# so the output of "$test" is "c:\test" instead of "c:\\test"
[^_^]PS[842]>$test = (pwd).path
[^_^]PS[843]>$test = Join-Path $test "\test"
[^_^]PS[844]>$test
C:\test
==============================
# Oneliner to do the same.
[^_^]PS[845]>$foo = Join-Path (pwd).path "test"
[^_^]PS[846]>$foo
C:\test
==============================


> I feel extremely stupid for asking this:


No, you shoudln't Everyone has to start somewhere

--
Sung M Kim

Please don''t bother me with spam...
My System SpecsSystem Spec
Old 08-28-2006   #4 (permalink)
James Truher


 
 

Re: Manipulating current directory as a variable

there should be an automatic variable $PWD which is the result of
get-location (which is a pathinfo), so you can what you want via:

$pwd.path + "/test"

PS C:\Documents and Settings\jimtru\Desktop> $pwd.path + "/test"
C:\Documents and Settings\jimtru\Desktop/test

--
James Truher[MSFT]
Program Manager - Windows PowerShell
Microsoft Corporation
This posting is provided "AS IS" with no warranties, no confers rights.
Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx
"Ralish" <Ralish@discussions.microsoft.com> wrote in message
news:2108D6A6-D292-4EA5-BE42-668F1E1307C8@microsoft.com...
>I feel extremely stupid for asking this:
>
> How does one set a variable to the current directory and manipulate it?
> This
> apparently can't be done with $test = pwd as on trying to manipulate it,
> e.g.
> $test = $test + "\test" an error is received:
>
> Method invocation failed because [System.Management.Automation.PathInfo]
> doesn't contain a method n
> amed 'op_Addition'.
> At line:1 char:16
> + $test = $test + <<<< "\test"
>
> Thanks in advance,
>
> Ralish
>
>



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Set a variable for the current domain PowerShell
Difference in getting the current exe directory .NET General
bug in join-path when the current directory is used PowerShell
how do I keep powershells current directory and dotnets current directory in sync PowerShell
Current script special variable PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46