Page 1 of 1
Is it possible to find the name of a parent script that has run your script by its bare name (i.e. without CALL) ?
Posted: 22 Jan 2023 20:12
by SandyClams
I've tried WMIC and the (goto) trick so far. WMIC, I have no idea how I would trace a ParentProcessID etc. to a file name and path even if I got it, but maybe I'm missing something. Meanwhile (goto) seems useless; I know how to find the parent if my script was CALLED, but it's all one call stack when the script is simply RUN. I know there's some wizards posting here, does anyone have any definitive information about whether this is even possible, or what approaches could be taken to do it? I will take any help I can get.
p.s., to me this distinction is clear, but just in case,
Code: Select all
@ECHO OFF
:: this is the "run" I'm hoping I can find the caller from
"C:\MY PATH\MYSCRIPT.BAT"
:: this one I already know how to find the caller from
CALL "C:\MY PATH\MYSCRIPT.BAT"
thanks for reading!
Re: Is it possible to find the name of a parent script that has run your script by its bare name (i.e. without CALL) ?
Posted: 23 Jan 2023 03:57
by OJBakker
This is chaining of batchfiles. The first one stops, and the second one starts.
Easy solution: add the name of the first one as parameter.
The second one will have the name of the first one in parameter %1.
Code: Select all
@ECHO OFF
:: this is the "run" I'm hoping I can find the caller from
"C:\MY PATH\MYSCRIPT.BAT" "%~nx0"
:: this one I already know how to find the caller from
CALL "C:\MY PATH\MYSCRIPT.BAT"
Re: Is it possible to find the name of a parent script that has run your script by its bare name (i.e. without CALL) ?
Posted: 23 Jan 2023 08:19
by SandyClams
yeah, I see your example here and also on my StackOverflow post, where I see you have also commented. Jeb commented there as well and agrees it's almost definitely impossible. I already kinda had a sense that it wouldn't be possible without supplying the name of the top level file as an argument when chaining the second file.
the specific reason I'm hoping to do it this way -- that is, to run the second file by its bare name with no further requirement of the top level script -- is because I'm planning to do introspective/preprocessing stuff to the top level script when it runs my nested script, and I was hoping for aesthetic reasons that in the top level script, I could disguise the run command completely and have it appear as a purely syntactic element. It shouldn't be too big a deal though if I need to compromise by using a CALL statement instead, or by supplying the top level's name as an argument.
thanks for your help!