![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | How can I tell my own function's name to call myself recursively? How do I get my function to call itself recursively. I suppose I could just hard-code my name in the function but I'm not sure that covers all cases. For example function Foo {.... ;foo -depth $d} function Bar { function foo{something} if ($x){foo}else{global:foo}} |
My System Specs![]() |
| | #2 (permalink) |
| | Re: How can I tell my own function's name to call myself recursively? On Aug 12, 3:02*pm, RickB <rbiel...@xxxxxx> wrote: Quote: > How do I get my function to call itself recursively. > > I suppose I could just hard-code my name in the function but I'm not > sure that covers all cases. > For example > > function Foo {.... ;foo -depth $d} > > function Bar { function foo{something} > if ($x){foo}else{global:foo}} function, but I can tell you that using the function name inside of the function definition works just fine for recursive functions. For example, this function below uses its own name to recursively name all files under a specific path # Recurse($path, $fileglob) # # Recurses through a psdrive and prints all items that match. # # Args: # [string]$path: The starting path # [string]$fileglob(optional): The search string for matching files # function Recurse ([string]$path, [string]$fileglob){ if (-not (Test-Path $path)) { Write-Error "$path is an invalid path." return $false } $files = @(dir -Path $path -Include $fileglob) foreach ($file in $files) { if ($file.GetType().FullName -eq 'System.IO.FileInfo') { Write-Output $file.FullName }elseif ($file.GetType().FullName -eq 'System.IO.DirectoryInfo') { Recurse $file.FullName } } } |
My System Specs![]() |
| | #3 (permalink) |
| | Re: How can I tell my own function's name to call myself recursively? On Aug 13, 12:55*am, Shay Levy [MVP] <n...@xxxxxx> wrote: Quote: > Get the name of the currently running function: > > function foo{ "function name: " + $myinvocation.InvocationName } > > --- > Shay Levy > Windows PowerShell MVPhttp://blogs.microsoft.co.il/blogs/ScriptFanatic > > R> How do I get my function to call itself recursively. > R> > R> I suppose I could just hard-code my name in the function but I'm not > R> sure that covers all cases. > R> For example > R> function Foo {.... ;foo -depth $d} > R> > R> function Bar { function foo{something} > R> if ($x){foo}else{global:foo}} do with myinvocation. What about the second part, though? Is there an advantage using that vs. just using the name for the purposes of recursion? |
My System Specs![]() |
| | #4 (permalink) |
| | Re: How can I tell my own function's name to call myself recursively? Get the name of the currently running function: function foo{ "function name: " + $myinvocation.InvocationName } --- Shay Levy Windows PowerShell MVP http://blogs.microsoft.co.il/blogs/ScriptFanatic R> How do I get my function to call itself recursively. R> R> I suppose I could just hard-code my name in the function but I'm not R> sure that covers all cases. R> For example R> function Foo {.... ;foo -depth $d} R> R> function Bar { function foo{something} R> if ($x){foo}else{global:foo}} |
My System Specs![]() |
| | #5 (permalink) |
| | Re: How can I tell my own function's name to call myself recursively? I'm not sure there's any advantage except for when you change your function name and don't update the change in your inner call. --- Shay Levy Windows PowerShell MVP http://blogs.microsoft.co.il/blogs/ScriptFanatic t> On Aug 13, 12:55 am, Shay Levy [MVP] <n...@xxxxxx> wrote: t> Quote: Quote: >> Get the name of the currently running function: >> >> function foo{ "function name: " + $myinvocation.InvocationName } >> >> --- >> Shay Levy >> Windows PowerShell >> MVPhttp://blogs.microsoft.co.il/blogs/ScriptFanatic >> R> How do I get my function to call itself recursively. >> R> >> R> I suppose I could just hard-code my name in the function but I'm >> not >> R> sure that covers all cases. >> R> For example >> R> function Foo {.... ;foo -depth $d} >> R> >> R> function Bar { function foo{something} >> R> if ($x){foo}else{global:foo}} t> do with myinvocation. What about the second part, though? Is there t> an advantage using that vs. just using the name for the purposes of t> recursion? t> |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Recursively check subdirectories | PowerShell | |||
| I'd like to edit a function's source file | PowerShell | |||
| Changing folder permissions recursively/ | Vista security | |||
| Recursively Deleting Shares | PowerShell | |||
| How have I gone wrong with a function's parameters list? | PowerShell | |||