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 - Getting/Setting filesystem working directory

Reply
 
Old 08-16-2006   #1 (permalink)
Alex K. Angelopoulos [MVP]


 
 

Getting/Setting filesystem working directory

Since there are some occasions where the location cmdlets don't work well -
for example, relative path names used in functions - I've got a couple of
functions I use to determine the working directory and set it. Note that if
Set-WorkingDirectory is invoked with no arguments, it synchronizes the
working directory with the PowerShell filesystem location.

function Get-WorkingDirectory
{
Param([switch]$Object)
$WorkDir = [IO.DirectoryInfo]([Environment]::CurrentDirectory)
if($Object){$WorkDir}else{$WorkDir.Fullname}
}

function Set-WorkingDirectory
{
Param([string]$Path = (Get-Location -PSProvider FileSystem))
[Environment]::CurrentDirectory = $Path
}



My System SpecsSystem Spec
Old 08-16-2006   #2 (permalink)
=?Utf-8?B?ZGFuY2UyZGll?=


 
 

RE: Getting/Setting filesystem working directory


"Alex K. Angelopoulos [MVP]" wrote:

> Since there are some occasions where the location cmdlets don't work well -

http://www.leeholmes.com/blog/SetLoc...Directory.aspx

Lee Holmes has a blog entry on that behavior on "Set-Location, and
[Environment]::CurrentDirectory"

====================
Sung M Kim

Please don''t bother me with spam...

My System SpecsSystem Spec
Old 08-16-2006   #3 (permalink)
Alex K. Angelopoulos [MVP]


 
 

Re: Getting/Setting filesystem working directory

"dance2die" <dance2die@discussions.microsoft.com> wrote in message
news:C0B29903-D4F5-4AB7-A522-D0D1A72DABCD@microsoft.com...
>
> "Alex K. Angelopoulos [MVP]" wrote:
>
>> Since there are some occasions where the location cmdlets don't work
>> well -

> http://www.leeholmes.com/blog/SetLoc...Directory.aspx
>
> Lee Holmes has a blog entry on that behavior on "Set-Location, and
> [Environment]::CurrentDirectory"
>


Actually, that blog entry motivated this post - and the related news post I
made about inconsistent resolution of "." - or at least the _experience_ of
it when we call a .NET method. I can't seem to resolve paths directly using
PathInfo or PathIntrinsics, and Resolve-Path is not very useful for guessing
at the path of an item that does not yet exist.

So far, the only reliable technique I've found is to use this:

function Get-AbsolutePath
{
Param([string]$name)
Resolve-Path $name -ev:errorOut -ea:SilentlyContinue;
if(!$?)
{
$file = New-Item -Path:$name -ItemType:file;
$path = $file.Fullname;
Remove-Item -Path:$file;
$path;
}
}

Using that, a function that deals with a file or folder can get either an
absolute or a relative PSPath from a user and correctly resolve it to a
fully-qualified path usable in .NET method calls. As it is, I occasionally
forget to do this and so I have to go to my home directory and clean out
accidentally created files at regular intervals...


My System SpecsSystem Spec
Old 08-16-2006   #4 (permalink)
=?Utf-8?B?ZGFuY2UyZGll?=


 
 

Re: Getting/Setting filesystem working directory


"Alex K. Angelopoulos [MVP]" wrote:

> Using that, a function that deals with a file or folder can get either an
> absolute or a relative PSPath from a user and correctly resolve it to a
> fully-qualified path usable in .NET method calls. As it is, I occasionally
> forget to do this and so I have to go to my home directory and clean out
> accidentally created files at regular intervals...


i am not sure if i understood the usage correctly but,
I usually do this to pass non-existent file names to .NET methods
by using combination of "test-path" and "resolve-path".

[^_^]PS[100]>rvpa .
Path
----
C:\programming\ps\test
[^_^]PS[101]>get-workingdirectory
c:\
[^_^]PS[102]>test-path "$(rvpa .)\bad-file.txt"
False
[^_^]PS[103]>"$(rvpa .)\bad-file.txt"
C:\programming\ps\test\bad-file.txt

Whether the file exists or not, i just simply pass "$(rvpa .)\file_name" to
the .NET method,
that way, you are essentially passing the full name so that .NET would not
have to resolve the file name any further through
[Environment]::CurrentDirectory.

> Resolve-Path is not very useful for guessing at the path of an item that does not yet exist.

By the way, i think "test-path" would come in more handy for testing for a
file that might/might not exist yet.

====================
Sung M Kim

Please don''t bother me with spam...

My System SpecsSystem Spec
Old 08-16-2006   #5 (permalink)
Alex K. Angelopoulos [MVP]


 
 

Re: Getting/Setting filesystem working directory


"dance2die" <dance2die@discussions.microsoft.com> wrote in message
news:606A6FED-FA75-429A-A5AB-3122576FE4E5@microsoft.com...
>
> "Alex K. Angelopoulos [MVP]" wrote:


> i am not sure if i understood the usage correctly but,


Yes, you are understanding what I mean; the solution (snipped) is useful.

> By the way, i think "test-path" would come in more handy for testing for a
> file that might/might not exist yet.


It is, but I would need to repeat the work. Here's an example of a scenario
I have in mind.

Someone begins using PowerShell, learns command basics, and is comfortable
with using Set-Location and commands like Get-ChildItem, Get-Item, and so
on. They know all about specifying paths, and even using relative path
symbols like . and ..

They can do this:
Set-Location C:\Temp
and then when they do this:
Get-ChildItem > .\list.txt
or this:
Get-ChildItem . | set-content list.txt
or this:
Get-ChildItem | Export-Csv list.csv
in every case, they will be listing the files within C:\temp and the output
will go to c:\temp\list.txt or c:\temp\list.csv.

Now let's say someone writes a function or script that takes a filesystem
path as an argument. My To-Stream function might be an example. I don't know
whether the person is trying to pass an absolute path or a relative path.
However, they will expect relative paths to work relative to one's location.
Furthermore, the person writing the function might not know about the
ramifications of this. If you run To-Stream as it is now, you get output
relative to the working directory, not the PS location.

I'm not sure what a good solution is, since we don't want to force
resolution of a PS location, for the reasons Lee points out. I think it
might be useful to declare a type of object called a PSPath that can be
resolved into a fully-qualified PowerShell-style path.

There's another minor aberration I've f


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Apply column headings setting in Windows Explorer to all directory Vista General
Setting up a directory and moving a file into that directory from Vista General
Setting up a directory and moving a file into that directory from Vista General
Setting all directory view settings the same. Vista file management
How to change ftp server directory security setting? Vista General


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