Page 1 of 1

Name of a function

Posted: 05 Oct 2013 07:22
by pumi
Hello,

how can I get the name of a function.

like this:

Code: Select all

:theFunction
   echo the name of the function is ???
goto:eof


thanks in advance, pumi

Re: Name of a function

Posted: 05 Oct 2013 07:30
by Adrianvdh
Hi
You would need to call the function

Code: Select all

call :theFunction

Re: Name of a function

Posted: 05 Oct 2013 07:31
by penpen
This should do it, if you have called it:

Code: Select all

@echo off
call :theFunction
goto:eof

:theFunction
   echo the name of the function is %0
goto:eof

penpen

Edit: Added code to full example.

Re: Name of a function

Posted: 05 Oct 2013 07:50
by pumi
thanks for your answer.
Can I get the name without the colon...like theFunction?

pumi

Re: Name of a function

Posted: 05 Oct 2013 07:58
by foxidrive
This should work:

Code: Select all

@echo off
call :theFunction
pause
goto:eof

:theFunction
   set func=%0
   echo the name of the function is %func:~1%
goto:eof




I thought this would work - interestingly it returns the main batch file path and name.

Code: Select all

@echo off
call :theFunction
pause
goto:eof

:theFunction
   echo the name of the function is %0 %~pnx0
goto:eof

Re: Name of a function

Posted: 05 Oct 2013 08:21
by pumi
I would like do this:

Code: Select all

@echo off
call :theFunction
echo theFunction
goto:eof

:theFunction
    set func=%0
    echo the name of the function is %func:~1%
    set "%func:~1%=the result"
goto:eof


I want to call the function and give a return value with the
same name as the function without the colon.
Unfortunately this does not work in this way.
Can you give me help again?
Thanks pumi

Re: Name of a function

Posted: 05 Oct 2013 08:32
by aGerman
The variable func is available in the main code as well.

Code: Select all

@echo off &setlocal

call :theFunction
echo back in main: %func%
pause
goto:eof

:theFunction
    set "func=%0"
    set "func=%func:~1%"
    echo the name of the function is %func%
goto:eof

Regards
aGerman

Re: Name of a function

Posted: 05 Oct 2013 09:24
by penpen
pumi wrote:Unfortunately this does not work in this way.
You have just missed the %'s:

Code: Select all

@echo off
call :theFunction
echo %theFunction%
goto:eof

:theFunction
    set func=%0
    echo the name of the function is %func:~1%
    set "%func:~1%=the result"
goto:eof

penpen

Re: Name of a function

Posted: 05 Oct 2013 10:16
by pumi
this works for me now, thanks guys