This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
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 Pathwhat 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-pathMSH>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\/\/