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
How was I called?
Moderator: DosItHelp
Command Line parameters
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.
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.
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
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
-
- Posts: 40
- Joined: 25 Jan 2008 14:05
What jep is suggesting is:
Output:
Enjoy
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
myWhoCalled called from elsewhere
myWhoCalled.baT called from within batch
Enjoy