/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" MSH MSN Instant messenger project (broken off) "," Running (Un-block) a downloaded script in MSH "," Timing the loading of your profile in MSH "," Tuning MSH in your Profile "," Einstein 2 + subtopic. "," Burn MSH ?? "," Monad and Adam (and a preview of my to-be AD provi... "," Windows MineSweeper cheat "," erroraction on new-item, my Wrong use of Stringbui... "," MSH Minesweeper GUI game "," ")

Monday, November 28, 2005

 


Working with a Path in MSH



In Monad there are some handy tools to work with a path,

I want to mention some I think are the most handy here.
Also I get to the differences between a path in MSH and in CMD, as in MSH it’s not always a disk we are referring to here, but more about that later.

First test-path

Test-path does what the most of you are used to do with IF EXIST in batch files.


MSH>test-path c:\test
True
MSH>test-path c:\foo
False
MSH>test-path c:\test\*.txt
True
MSH>test-path c:\test\* -ex *.txt
True
MSH>test-path c:\test\*.* -ex *.txt
False


The last 2 are interesting, the question asked to test-path is here :
The first is Does c:\test contain any Items other then text files ?
(in my case there was a subdirectory of test)

Then I nice one I found in Resolve Path
what does this do ?


resolve-path ~
resolves the users Home directory ;-)
(I don’t know where the ~ comes from, It does not work on his own, nor have I seen it anywhere else yet.)


MSH>resolve-path ~

Path
----
C:\Documents and Settings\mow

And another nice one :


MSH>resolve-path ~).drive.CurrentLocation
Test
get the current location on the drive(provider) where the homedirectory is.
Very useful ;-)
But more serious .. the point here is that you get access to the Drive Object …
And you can also use wildcards here to get more objects back.


MSH>resolve-path g:\mon*\t*\*.*

Path
----
G:\Monad\test...test\test.xml
G:\Monad\txt\com.txt
G:\Monad\txt\count.txt
A bit more usefull ?

Combine-path


MSH>combine-path c:\test\ \monad
c:\test\monad
MSH>combine-path c:\test\ /monad
c:\test\monad


as you can see in this example this is more safe than just to use a string for this, as this class has some logic (it sees that both parts contain the delimiter and removes one)

Also, remember a drive in Monad is actually a Provider, so to be save use the combine-path command for this, then you use the MshPath as the glue, (This is also the wrapper that provides the use of / in MSH), before use the path gets resolved to a format the Provider is familiar with.

See also the difference here :


MSH>(gi .).MshPath
FileSystem::C:\Test
MSH>cd hklm: ; (gi .).MshPath
Registry::HKEY_LOCAL_MACHINE
MSH>pwd

Path
----
HKLM:
MSH>convert-path (pwd)
HKEY_LOCAL_MACHINE\


So convert-path will give you the path as it is presented to the provider.

I only tipped at some of the uses of the Path commandlets and the difference between a path in MSH and in CMD, i would reccomend you look at there help a bit, and in the beta documentation (see $links), and I would like to give you some other examples of working with a path in MSH.


MSH>pwd

Path
----
C:\Test\Monad


MSH>(pwd).path
C:\Test\Monad
MSH>(pwd).path.length
13
MSH>(gi .).root.name
C:MSH>(gi .).parent.name
Test
MSH>(gi .).parent.name.StartsWith("t")
False
MSH>(gi .).parent.name.StartsWith("T")
True





maybe some thinks will look a bit strange at first, but if you play a bit with them they are prety powerfull tools,

Some Tips are don't think to much filesystem think Provider and don't use test parsing if you don't realy need it (think Oject ;-).
As always with Monad using get-member a lot will help also .

Enjoy,

Gr /\/\o\/\/


Comments: Post a Comment



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?