Page 1 of 1

How was I called?

Posted: 16 Oct 2007 09:12
by bbowler
Is there a way for a batch file to know whether it was called from a command window or from within another batch file?

I have a script that should prompt if run "interactively" or not prompt if "call"ed from a within another batch file.

Thanks!
Bruce

Posted: 17 Oct 2007 14:23
by jeb
Hi bowler,

I dont know a way to detect this.

But perhaps you can use simple a special start parameter for the batch file,
if it is calling from another batch file.

Command Line parameters

Posted: 19 Oct 2007 05:50
by bsod
Use a command line parameter from the first batch and check for it in the second batch

eg one batch calls another
1stbatch.bat contains
IF blah blah blah DO START /wait 2ndbatch.bat called

2ndbatch.bat contains
IF "%1"=="called" GOTO blah blah blah

If 2ndbatch.bat is run on its own without a parameter then it will fail the if statement. :shock:

Posted: 19 Oct 2007 07:21
by bbowler
I was hoping to avoid passing yet another parameter as the called procedure already has up to 5 different params.

Oh well...

Posted: 19 Oct 2007 13:26
by jeb
Hi bbowler,

you can try to use %0

if you start the batch (doIt.bat) from command line
like doIt or doIt.bat you get doIt or doIt.bat
if you call it from another batch call it like doIt.BaT
So you can simple decide by the case of "BaT" how it is called.
Not perfect, but perhaps better than the "parameter way".

jeb

Posted: 28 Jan 2008 12:56
by jaffamuffin
You could start the 2nd batch in the first batch like this:

SET called=1
call 2ndbatch.baT




2ndbatch.bat
IF called=1 then bleh
otherwise it was ran from the cmd prompt.

Last line in 2ndbatch:

set called=0

Posted: 30 Jan 2008 23:58
by DosItHelp
What jep is suggesting is:

Code: Select all

@echo off

set "me=%~0"
if "%me:~-4%"==".baT" (
    echo.%~0 called from within batch
    GOTO:EOF
)

echo.%~0 called from elsewhere
myWhoCalled.baT
Output:
myWhoCalled called from elsewhere
myWhoCalled.baT called from within batch

Enjoy :wink: