Name of a function

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pumi
Posts: 15
Joined: 05 Oct 2013 06:48
Location: Germany, BW

Name of a function

#1 Post by pumi » 05 Oct 2013 07:22

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

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: Name of a function

#2 Post by Adrianvdh » 05 Oct 2013 07:30

Hi
You would need to call the function

Code: Select all

call :theFunction

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Name of a function

#3 Post by penpen » 05 Oct 2013 07:31

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.

pumi
Posts: 15
Joined: 05 Oct 2013 06:48
Location: Germany, BW

Re: Name of a function

#4 Post by pumi » 05 Oct 2013 07:50

thanks for your answer.
Can I get the name without the colon...like theFunction?

pumi

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Name of a function

#5 Post by foxidrive » 05 Oct 2013 07:58

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

pumi
Posts: 15
Joined: 05 Oct 2013 06:48
Location: Germany, BW

Re: Name of a function

#6 Post by pumi » 05 Oct 2013 08:21

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

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Name of a function

#7 Post by aGerman » 05 Oct 2013 08:32

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

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Name of a function

#8 Post by penpen » 05 Oct 2013 09:24

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

pumi
Posts: 15
Joined: 05 Oct 2013 06:48
Location: Germany, BW

Re: Name of a function

#9 Post by pumi » 05 Oct 2013 10:16

this works for me now, thanks guys

Post Reply