![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) |
| 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. 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:</FONT></DIV> > <DIV><FONT face=Arial size=2></FONT> </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"> <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"> {</PRE><PRE style="MARGIN: 0px"> <SPAN style="COLOR: blue">string</SPAN> urpath = <SPAN style="COLOR: blue">null</SPAN>;</PRE><PRE style="MARGIN: 0px"> <SPAN style="COLOR: green">// This is a hack for now until I can figure out a better way. GetUnresolved... doesn't like</SPAN></PRE><PRE style="MARGIN: 0px"> <SPAN style="COLOR: green">// being passed a path with wildcard characters.</SPAN></PRE><PRE style="MARGIN: 0px"> <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"> {</PRE><PRE style="MARGIN: 0px"> urpath = GetUnresolvedProviderPathFromPSPath(path);</PRE><PRE style="MARGIN: 0px"> }</PRE><PRE style="MARGIN: 0px"> </PRE><PRE style="MARGIN: 0px"> <SPAN style="COLOR: blue">if</SPAN> (!<SPAN style="COLOR: teal">String</SPAN>.IsNullOrEmpty(urpath) && !<SPAN style="COLOR: teal">File</SPAN>.Exists(urpath))</PRE><PRE style="MARGIN: 0px"> {</PRE><PRE style="MARGIN: 0px"> WriteDebug(<SPAN style="COLOR: maroon">"Unresolved path is "</SPAN> + urpath);</PRE><PRE style="MARGIN: 0px"> CreateFile(urpath);</PRE><PRE style="MARGIN: 0px"> }</PRE><PRE style="MARGIN: 0px"> <SPAN style="COLOR: blue">else</SPAN></PRE><PRE style="MARGIN: 0px"> {</PRE><PRE style="MARGIN: 0px"> <SPAN style="COLOR: teal">ProviderInfo</SPAN> providerInfo;</PRE><PRE style="MARGIN: 0px"> <SPAN style="COLOR: teal">Collection</SPAN><<SPAN style="COLOR: blue">string</SPAN>> rpaths = GetResolvedProviderPathFromPSPath(path, <SPAN style="COLOR: blue">out</SPAN> providerInfo);</PRE><PRE style="MARGIN: 0px"> <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"> {</PRE><PRE style="MARGIN: 0px"> WriteDebug(<SPAN style="COLOR: maroon">"Resolved path is "</SPAN> + rpath);</PRE><PRE style="MARGIN: 0px"> ChangeFileTimes(rpath);</PRE><PRE style="MARGIN: 0px"> }</PRE><PRE style="MARGIN: 0px"> }</PRE><PRE style="MARGIN: 0px"> }</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> </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". 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 '*'?</FONT></DIV> > <DIV><FONT face=Arial size=2></FONT> </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> </DIV> > <DIV><FONT face=Arial size=2></FONT> </DIV> > <DIV><FONT face=Arial size=2></FONT> </DIV></BODY></HTML> > > ------=_NextPart_000_0050_01C6D500.7FB51D10-- |
My System Specs![]() |
| | #3 (permalink) |
| 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 Specs![]() |
| | #4 (permalink) |
| 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 Specs![]() |
| | #5 (permalink) |
| 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 Specs![]() |
![]() |
| 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 |