Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - How can I tell my own function's name to call myself recursively?

Reply
 
Old 08-12-2008   #1 (permalink)
RickB


 
 

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 SpecsSystem Spec
Old 08-12-2008   #2 (permalink)
tojo2000


 
 

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}}
I haven't found a good way to get the name of the currently running
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 SpecsSystem Spec
Old 08-13-2008   #3 (permalink)
tojo2000


 
 

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}}
Sweet. I was trying to figure it out, and I knew it had something to
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 SpecsSystem Spec
Old 08-13-2008   #4 (permalink)
Shay Levy [MVP]


 
 

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 SpecsSystem Spec
Old 08-13-2008   #5 (permalink)
Shay Levy [MVP]


 
 

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> Sweet. I was trying to figure it out, and I knew it had something to
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 SpecsSystem Spec
Reply

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


Vista Forums 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 Ltd

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