Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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.

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

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

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.

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.

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.

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.

%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.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |