Computing.Net > Forums > Programming > Check if in a subrutine

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Check if in a subrutine

Reply to Message Icon

Name: Axachi
Date: October 14, 2009 at 01:47:42 Pacific
OS: Windows 2003 Server
CPU/Ram: 4gb
Product: Microsoft Windows server 2003 r2 standard edition w/sp2
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: October 14, 2009 at 02:36:22 Pacific
Reply:

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.


0

Response Number 2
Name: Judago
Date: October 14, 2009 at 02:43:55 Pacific
Reply:

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


0

Response Number 3
Name: Judago
Date: October 14, 2009 at 02:52:18 Pacific
Reply:

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


0

Response Number 4
Name: klint
Date: October 14, 2009 at 06:46:41 Pacific
Reply:

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


0

Response Number 5
Name: IVO
Date: October 14, 2009 at 09:05:56 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: klint
Date: October 14, 2009 at 14:30:49 Pacific
Reply:

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.


0

Response Number 7
Name: Axachi
Date: October 20, 2009 at 02:50:58 Pacific
Reply:

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

Response Number 8
Name: Axachi
Date: October 20, 2009 at 03:19:27 Pacific
Reply:

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


0

Response Number 9
Name: Razor2.3
Date: October 20, 2009 at 16:43:17 Pacific
Reply:

What are you trying to accomplish, exactly?


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Check if in a subrutine

checking if a form is open in vb www.computing.net/answers/programming/checking-if-a-form-is-open-in-vb/2207.html

C++ can you check if a file exists? www.computing.net/answers/programming/c-can-you-check-if-a-file-exists/11411.html

need help with check boxes in VB www.computing.net/answers/programming/need-help-with-check-boxes-in-vb/11815.html