Check if in a subrutine

Microsoft Windows server 2003 r2 standar...
October 14, 2009 at 01:47:42
Specs: Windows 2003 Server, 4gb
Hi I'm trying to get a batch to check with a if
statment that I'm in the subrutine or not..
Her is what i hope to achive..
a command calls a cleanup sub.
CALL :SUB_Clean

:SUB_Clean
CALL :SUB_PING (this checks if host is alive)
DEL somefile
GOTO :EOF

:SUB_PING
ping /l 1 /n 5 %HOST%>NUL
now I want to log to to different logfiles
depending on what subrutine I started this from
somthing like:
if "CALL" == ":SUB_Clean" echo the files are
now cleand>>%CleanLOG%
if "CALL" == ":SUB_FTP" echo This files
where on the ftp>>%LOG%
GOTO :EOF


See More: Check if in a subrutine

Report •


#1
October 14, 2009 at 02:36:22
Inside the subroutine code

  if "%0"==":SUB" ...

  if "%0"==":Sub_Name" ...

while in the main %0 returns the batch filename.

An internal subroutine is interpreted and developed by cmd.exe as a "virtual" batch so the %0 external variable works fine.


Report •

#2
October 14, 2009 at 02:43:55
I don't think there is a way to check where the subroutine was called from......

Perhaps you could pass a filename and a switch as parameters:

call :SUB_PING "c:\logfile" ftp
goto :eof

:SUB_PING
ping /l 1 /n 5 %HOST%>NUL
if /i "%~2" == "subclean" >> "%~1" echo the files are
now cleand
if /i "%~2" == "FTP" >> "%~1" echo This files
where on the ftp
GOTO :EOF


Batch Variable how to


Report •

#3
October 14, 2009 at 02:52:18
Hello IVO,

%0 will only get the current label name, I'm pretty sure the op want the label name it was called from.

Perhaps this leads to a better solution, simply calling the label with %0

:SUB_Clean
CALL :SUB_PING "%~0"
DEL somefile
GOTO :EOF

:SUB_PING
ping /l 1 /n 5 %HOST%>NUL
now I want to log to to different logfiles
depending on what subrutine I started this from
somthing like:
if "%~1" == ":SUB_Clean" echo the files are
now cleand>>%CleanLOG%
if "%~1" == ":SUB_FTP" echo This files
where on the ftp>>%LOG%
GOTO :EOF


Batch Variable how to


Report •

Related Solutions

#4
October 14, 2009 at 06:46:41
The above will work, but I would say it's not good programming style to force your subroutines to know about all of its possible callers. For a start, that would make the subroutine callable from only a specified set of callers, instead of being self-sufficient. So if you decide the call the subroutine from another place in a future version, it won't work. It's better to pass arguments to tell the subroutine which filename to use for logging, what message to write, etc.

http://en.wikipedia.org/wiki/Coupli...


Report •

#5
October 14, 2009 at 09:05:56
Hi Judago and klint,

your appropriate comments rise me the following question:

More and more posts are developed by the (highly) skilled regulars while the OP vanishes as he/she is no more interested in his/her own issue.

This is frustrating and leads me to suppose there is no real interest in the response. Your comments and advices are indeed worth to be considered, but a ugly feeling remains in the background.


Report •

#6
October 14, 2009 at 14:30:49
Ivo, I think what you say is true in many cases, but it is still
worth discussing a point. The OP is free to read it or ignore it.
But these forums remain here for all to see in the future. So the
answers don't just help the OP, but they also help anyone in
future looking for an answer for their issue by doing a search on
some relevant keywords. And these people can see, all at once,
a complete discussion of the advantages and disadvantages of
all proposed solutions to their problem.

Report •

#7
October 20, 2009 at 02:50:58
Wow guys I take a bow to you. Now I need to try out some of this. I let you know what I get working.

Sorry IVO for not responding. but Iv been sick.. and my kids aswell.


Report •

#8
October 20, 2009 at 03:19:27
%0 will only get the current label name, I'm pretty sure the op want the label name it was called from.
:Judago you are right That is what I wanted.

But I agree with Klint as well. I know see it's gonna be hard. So maby the best thing would be to put the ping routine into each sub routine..? Thank youall for the help.


Report •

#9
October 20, 2009 at 16:43:17
What are you trying to accomplish, exactly?

Report •


Ask Question