By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

where was bat file called from?

4nt8 or ttc le 13

I have a bat file called from many other bat files..

Is there a way to determine, from within the bat file the name of the bat file from which it was called?

Or alternatively when one quits the bat file can one find out where it's returning to?
Guess there's got to be some sort of stack inside 4nt or tcc where this is kept?

PS There's %~dpnx0 that I didn't know about that tells one the name of the bat file, which I just discovered from googling my problem.

%_BATCHNAME gives the full name of the current batch file. But I don't think there is any way to read the CALL stack.
%_BATCHNAME gives the full name of the current batch file. But I don't think there is any way to read the CALL stack.

Thanks - pity, because it'd have been relatively easy I guess when implementing to put it as one of the %x things! Not sure where to look Suppose this data is in memory, not a file, so can't try eg using grep to hack/find it?

Any ideas welcome...

Stash it in an environment variable before you call the second batch file?

set caller=%_batchname
call c:\path\to\mysub.btm
Trouble is there are lots,100? , of places where it's called from. Maybe I could alias call to `set caller=%_batchname^call` - this would be overhead on all calls -not sure whether that'd matter, but suppose could just alias call my_prog=`set caller=%_batchname^call my_prog`. Not sure whether this'd work...

Still be better if there was a variable called _caller or similar. How about this for next version of tcc.
I'm trying to figure out how to implement this as a plugin. You could intercept CALL easily enough, and push the current batch name onto a stack. But how to know when to pop it off again? Intercept QUIT and CANCEL too -- but TCC can also return from a CALLed batch simply by running off the end of the file. How to detect that? And then there's labeled subroutines, which can be started by either CALL or GOSUB, and ended by QUIT, CANCEL, or EXIT, or the end of the file. Second-guessing the CALL stack would be a Lovecraftian monstrosity.
I don't need a 100% fix so maybe plugin is ott for my problem- I only use call filename and gosubs inside file. The nice thing about aliasing call is that it doesn''t matter about the exits.

Tried alias call=`set caller=%_batchname^call` - seem to get loop when I try it (example below)- know why? Maybe it is because I have lots of other stuff running? Anyway to fix?

tmp.bat =
@echo off
call l

@echo off
echo --%_batchname called by:%caller

Your alias uses CALL. Which is an alias, which uses CALL. Which is an alias, which uses CALL, which is an alias....
You can prevent the problem like this:
alias call='set caller=%_batchname %+ *call'

The asterisk disables alias expansion for that one command.
Thanks that fixes it. I'm thinking of using this just as debug aid so not permanently. I run some scripts in background using 'start' so if I set it in my startup scripts, assume it'll effect all calls so suppose this'd be some sort of performance impact invoking the alias code for every call. Guess time will tell whether this'll matter. Any ideas welcome.

I do use gosub "filename" label too in few places so suppose I could use same idea with gosub ie
alias gosub=`set caller=%_batchname^*gosub` - would this work too? Suppose overhead might be greater tho' since there are an awful lot of gosubs, cf calls .

You mentioned other ways of entering a subroutine? Could I extend alias idea to these

Thanks for your help