1

Is there a way that a batch file called from another file can halt execution of the parent without closing the entire terminal session?

Something along the lines of this, which would require an admin to run the script:

@echo off
 :: mybatch.bat
call admintest.bat
echo I am still running
@echo off
:: admintest.bat
net session >nul 2>&1
if not %errorlevel% == 0 (
    echo Please run this script as admin
    :: Command to halt both this and the calling script
)

The goal would be for the parent script to not echo "I am still running" if the user running the parent script is not an admin. This is mainly to allow for easier maintenance, as instead of having to include the contents of admintest.bat in every script, you could just call it and quit as needed. Of course in this example admintest.bat has only a few lines of code, but the child script could be larger.

Compo
  • 36,585
  • 5
  • 27
  • 39
Osxtra
  • 21
  • 1
  • 3
  • If the idea is to close the parent script, use taskkill to end the parent using the title of the parent and the process id - the title can be passed as a parmeter to the child when called. – T3RR0R Feb 18 '20 at 13:47
  • @T3RR0R Using taskkill would close the terminal window, too – jeb Feb 18 '20 at 14:00
  • replace `:: Command to halt both this and the calling script` with `exit /b 1` and add `if errorlevel 1 exit /b` before the line `echo I am still running` – Stephan Feb 18 '20 at 14:30
  • @jeb `you could just call it and quit as needed.` this component of the question results in ambiguity regarding intent, compared to the question title - I read it as the OP wanting to quit the parent from within the child , then close the child - when admin status is lacking. – T3RR0R Feb 18 '20 at 14:55
  • @Stephan, That only works for simple cases, with nested calls in the `admintest.bat ` it's get messy – jeb Feb 18 '20 at 15:06

0 Answers0