How was I called?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bbowler
Posts: 2
Joined: 16 Oct 2007 09:07

How was I called?

#1 Post by bbowler » 16 Oct 2007 09:12

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#2 Post by jeb » 17 Oct 2007 14:23

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.

bsod
Posts: 6
Joined: 19 Oct 2007 05:21

Command Line parameters

#3 Post by bsod » 19 Oct 2007 05:50

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:

bbowler
Posts: 2
Joined: 16 Oct 2007 09:07

#4 Post by bbowler » 19 Oct 2007 07:21

I was hoping to avoid passing yet another parameter as the called procedure already has up to 5 different params.

Oh well...

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#5 Post by jeb » 19 Oct 2007 13:26

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

jaffamuffin
Posts: 40
Joined: 25 Jan 2008 14:05

#6 Post by jaffamuffin » 28 Jan 2008 12:56

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

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#7 Post by DosItHelp » 30 Jan 2008 23:58

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:

Post Reply