Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Cmdlet - Using GetResolvedProviderPathFromPSPath vs GetUnresolvedProviderPathFromPSPath

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 09-10-2006   #1 (permalink)
Keith Hill [MVP]
Guest


 

Cmdlet - Using GetResolvedProviderPathFromPSPath vs GetUnresolvedProviderPathFromPSPath

The docs on these two functions on the MSDN site aren't real illuminating. From some experimentation I've had to introduce a hack into my code that I would like to get rid of. Here's the code:

protected override void ProcessRecord(){ foreach (string path in _paths) { string urpath = null; // This is a hack for now until I can figure out a better way. GetUnresolved... doesn't like // being passed a path with wildcard characters. if (path.IndexOfAny(new char[] {'*', '?'}) == -1) { urpath = GetUnresolvedProviderPathFromPSPath(path); } if (!String.IsNullOrEmpty(urpath) && !File.Exists(urpath)) { WriteDebug("Unresolved path is " + urpath); CreateFile(urpath); } else { ProviderInfo providerInfo; Collection<string> rpaths = GetResolvedProviderPathFromPSPath(path, out providerInfo); foreach (string rpath in rpaths) { WriteDebug("Resolved path is " + rpath); ChangeFileTimes(rpath); } } }}
The problem I run into is that GetResolved* doesn't like it if I pass it a file name that doesn't exist which happens when you do a "Touch-File newfile.txt". That's fine, it seems that GetUnresolved* works in this case. However GetUnresolved* really doesn't like to see any wildcard characters. Seems I have a bit of a catch 22 that I'm working around with the hack of checking for wildcard characters. Is there a better way to do this? If not are there any other wildcard chars besides '?' and '*'?

--
Keith




My System SpecsSystem Spec
Old 09-11-2006   #2 (permalink)
klumsy@xtra.co.nz
Guest


 

Re: Cmdlet - Using GetResolvedProviderPathFromPSPath vs GetUnresolvedProviderPathFromPSPath

can't you do fullregex for wildcards... like
dir c:\[p-z]*




Keith Hill [MVP] wrote:
> The docs on these two functions on the MSDN site aren't real illuminating. From some experimentation I've had to introduce a hack into my code that I would like to get rid of. Here's the code:
>
> protected override void ProcessRecord(){ foreach (string path in _paths) { string urpath = null; // This is a hack for now until I can figure out a better way. GetUnresolved... doesn't like // being passed a path with wildcard characters. if (path.IndexOfAny(new char[] {'*', '?'}) == -1) { urpath = GetUnresolvedProviderPathFromPSPath(path); } if (!String.IsNullOrEmpty(urpath) && !File.Exists(urpath)) { WriteDebug("Unresolved path is " + urpath); CreateFile(urpath); } else { ProviderInfo providerInfo; Collection<string> rpaths = GetResolvedProviderPathFromPSPath(path, out providerInfo); foreach (string rpath in rpaths) { WriteDebug("Resolved path is " + rpath); ChangeFileTimes(rpath); } } }}
> The problem I run into is that GetResolved* doesn't like it if I pass it a file name that doesn't exist which happens when you do a "Touch-File newfile.txt". That's fine, it seems that GetUnresolved* works in this case. However GetUnresolved* really doesn't like to see any wildcard characters. Seems I have a bit of a catch 22 that I'm working around with the hack of checking for wildcard characters. Is there a better way to do this? If not are there any other wildcard chars besides '?' and '*'?
>
> --
> Keith
>
>
>
> ------=_NextPart_000_0050_01C6D500.7FB51D10
> Content-Type: text/html; charset=iso-8859-1
> Content-Transfer-Encoding: quoted-printable
> X-Google-AttachSize: 5814
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML><HEAD>
> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
> <META content="MSHTML 6.00.5450.4" name=GENERATOR>
> <STYLE></STYLE>
> </HEAD>
> <BODY>
> <DIV><FONT face=Arial size=2>The docs on these two functions on the MSDN site
> aren't real illuminating.&nbsp; From some experimentation I've had to introduce
> a hack into my code that I would like to get rid of.&nbsp; Here's the
> code:</FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2>
> <DIV
> style="FONT-SIZE: 8pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Couriew New"><PRE style="MARGIN: 0px"><DIV style="FONT-SIZE: 8pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Couriew New"><PRE style="MARGIN: 0px"><SPAN style="COLOR: blue">protected</SPAN> <SPAN style="COLOR: blue">override</SPAN> <SPAN style="COLOR: blue">void</SPAN> ProcessRecord()</PRE><PRE style="MARGIN: 0px">{</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">foreach</SPAN> (<SPAN style="COLOR: blue">string</SPAN> path <SPAN style="COLOR: blue">in</SPAN> _paths)</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; {</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">string</SPAN> urpath = <SPAN style="COLOR: blue">null</SPAN>;</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// This is a hack for now until I can figure out a better way.&nbsp; GetUnresolved... doesn't like</SPAN></PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// being passed a path with wildcard characters.</SPAN></PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">if</SPAN> (path.IndexOfAny(<SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">char</SPAN>[] {<SPAN style="COLOR: maroon">'*'</SPAN>, <SPAN style="COLOR: maroon">'?'</SPAN>}) == -1)</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; urpath = GetUnresolvedProviderPathFromPSPath(path);</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</PRE><PRE style="MARGIN: 0px">&nbsp;</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">if</SPAN> (!<SPAN style="COLOR: teal">String</SPAN>.IsNullOrEmpty(urpath) &amp;&amp; !<SPAN style="COLOR: teal">File</SPAN>.Exists(urpath))</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WriteDebug(<SPAN style="COLOR: maroon">"Unresolved path is "</SPAN> + urpath);</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; CreateFile(urpath);</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">else</SPAN></PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <SPAN style="COLOR: teal">ProviderInfo</SPAN> providerInfo;</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <SPAN style="COLOR: teal">Collection</SPAN>&lt;<SPAN style="COLOR: blue">string</SPAN>&gt; rpaths = GetResolvedProviderPathFromPSPath(path, <SPAN style="COLOR: blue">out</SPAN> providerInfo);</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">foreach</SPAN> (<SPAN style="COLOR: blue">string</SPAN> rpath <SPAN style="COLOR: blue">in</SPAN> rpaths)</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WriteDebug(<SPAN style="COLOR: maroon">"Resolved path is "</SPAN> + rpath);</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ChangeFileTimes(rpath);</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</PRE><PRE style="MARGIN: 0px">&nbsp;&nbsp;&nbsp; }</PRE><PRE style="MARGIN: 0px">}</PRE></DIV><!--EndFragment--></FONT><FONT face=Arial size=2></FONT><FONT face=Arial size=2></FONT></PRE></DIV></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2>The problem I run into is that GetResolved* doesn't
> like it if I pass it a file name that doesn't exist which happens when you do a
> "Touch-File newfile.txt".&nbsp; That's fine, it seems that GetUnresolved* works
> in this case.&nbsp; However GetUnresolved* really doesn't like to see any
> wildcard characters.&nbsp; Seems I have a bit of a catch 22 that I'm working
> around with the hack of checking for wildcard characters.&nbsp; Is there a
> better way to do this?&nbsp; If not are there any other wildcard chars besides
> '?' and '*'?</FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2>--</FONT></DIV>
> <DIV><FONT face=Arial size=2>Keith</FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV></BODY></HTML>
>
> ------=_NextPart_000_0050_01C6D500.7FB51D10--


My System SpecsSystem Spec
Old 09-11-2006   #3 (permalink)
Lee Holmes [MSFT]
Guest


 

Re: Cmdlet - Using GetResolvedProviderPathFromPSPath vs GetUnresolvedProviderPathFromPSPath

What do you mean that GetUnresolved... it doesn't like it?

PS:126 >
$executionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("foo*")
D:\oob\admin\monad\src\foo*

You'll definitely have a problem creating a file like that, though, as it
contains characters that are invalid for the filesystem.

What you probably want to do is this:

1) If the path contains wildcard characters, then resolve them using
GetResolved....
2) Otherwise, call GetUnresolved... and create the file.

To determine if a path contains wildcard characters, you can use:

PS:131 >
[System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters("foo*")
True

--
Lee Holmes [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

"Keith Hill [MVP]" <r_keith_hill@mailhot.moc.nospam> wrote in message
news:u7qfHLT1GHA.4264@TK2MSFTNGP05.phx.gbl...
The docs on these two functions on the MSDN site aren't real illuminating.
From some experimentation I've had to introduce a hack into my code that I
would like to get rid of. Here's the code:

protected override void ProcessRecord(){ foreach (string path in _paths)
{ string urpath = null; // This is a hack for now until I can
figure out a better way. GetUnresolved... doesn't like // being
passed a path with wildcard characters. if (path.IndexOfAny(new
char[] {'*', '?'}) == -1) { urpath =
GetUnresolvedProviderPathFromPSPath(path); } if
(!String.IsNullOrEmpty(urpath) && !File.Exists(urpath))
{ WriteDebug("Unresolved path is " + urpath);
CreateFile(urpath); } else { ProviderInfo
providerInfo; Collection<string> rpaths =
GetResolvedProviderPathFromPSPath(path, out providerInfo);
foreach (string rpath in rpaths) {
WriteDebug("Resolved path is " + rpath);
ChangeFileTimes(rpath); } } }}
The problem I run into is that GetResolved* doesn't like it if I pass it a
file name that doesn't exist which happens when you do a "Touch-File
newfile.txt". That's fine, it seems that GetUnresolved* works in this case.
However GetUnresolved* really doesn't like to see any wildcard characters.
Seems I have a bit of a catch 22 that I'm working around with the hack of
checking for wildcard characters. Is there a better way to do this? If not
are there any other wildcard chars besides '?' and '*'?

--
Keith




My System SpecsSystem Spec
Old 09-11-2006   #4 (permalink)
Keith Hill [MVP]
Guest


 

Re: Cmdlet - Using GetResolvedProviderPathFromPSPath vs GetUnresolvedProviderPathFromPSPath

"Lee Holmes [MSFT]" <lee.holmes@online.microsoft.com> wrote in message
news:epGxFuc1GHA.1040@TK2MSFTNGP06.phx.gbl...
> What you probably want to do is this:
>
> 1) If the path contains wildcard characters, then resolve them using
> GetResolved....
> 2) Otherwise, call GetUnresolved... and create the file.

Yep that's what I'm doing.

> To determine if a path contains wildcard characters, you can use:
>
> PS:131 >
> [System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters("foo*")
> True

Excellent. That's much better than my hack.

Thanks,
Keith


My System SpecsSystem Spec
Old 09-11-2006   #5 (permalink)
Keith Hill [MVP]
Guest


 

Re: Cmdlet - Using GetResolvedProviderPathFromPSPath vs GetUnresolvedProviderPathFromPSPath

<klumsy@xtra.co.nz> wrote in message
news:1157983762.241490.82090@e3g2000cwe.googlegroups.com...
> can't you do fullregex for wildcards... like
> dir c:\[p-z]*

Yep. It seems that Lee has the way to do this by asking PowerShell to tell
me what the wildcard chars are.

--
Keith


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Invoking Cmdlet Get-Location from cmdlet,cant get Currnt Directory Vikram PowerShell 2 06-05-2008 04:41 AM
Re: How to run following CmdLet using .NET John Vottero PowerShell 1 01-06-2008 09:23 PM
Whether a cmdlet derives from cmdlet or pscmdlet Marco Shaw PowerShell 1 09-19-2007 09:18 PM
Invoking a cmdlet from another cmdlet Marco Shaw PowerShell 2 09-19-2007 12:46 PM
Invoke Cmdlet from a Cmdlet =?Utf-8?B?ZnV6enkzMzM=?= PowerShell 3 08-25-2006 07:49 AM


Vistax64.com 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 2005-2008

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 47 48 49 50 51