Computer Hope

Microsoft => Microsoft DOS => Topic started by: livemoonkey on February 28, 2018, 05:32:29 PM

Title: Need .bat to launch 2 programs. P1 exits after P2 is closed.
Post by: livemoonkey on February 28, 2018, 05:32:29 PM
PLEASE HELP!

I want to create a batch (.bat) file that:

1:  Launch two applications.
2:  Application 1 closes automatically after application 2 is exited manually.

(Using Windows 10... if this info helps)

Thank you an advance!
Title: Re: Need .bat to launch 2 programs. P1 exits after P2 is closed.
Post by: nil on March 01, 2018, 09:04:25 AM
Give this a shot..
Code: [Select]
@echo off
set prog1=mspaint.exe
set prog2=notepad.exe
set killcmd=taskkill /f /im

echo "Starting %prog1% and %prog2%..."
start %prog1%
start %prog2%

:check
echo Checking if Program2 (%prog2%) is running...
tasklist /FI "imagename eq %prog2%" | find /I "%prog2%"
echo %ERRORLEVEL%
if ERRORLEVEL 1 (
  echo "Program2 (%prog2%) not found in task list. It is not running."
  goto notrunning
  )
if ERRORLEVEL 0 (
  echo "Program2 (%prog2%) found in tasklist. It is running."
  goto running
  )

:running
echo "Waiting before next check.."
:: Wait 4000ms (4s). Other methods of waiting: http://www.robvanderwoude.com/wait.php
PING 1.1.1.1 -n 1 -w 4000 >NUL
goto check

:notrunning
echo "Stopping Program1 (%prog1%)..."
@echo on
%killcmd% %prog1%
:: The end
Title: Re: Need .bat to launch 2 programs. P1 exits after P2 is closed.
Post by: livemoonkey on March 05, 2018, 06:47:45 PM
WOW!  Looks quite complicated! LOL!  Thank you for the quick response!

Well it worked exactly how I needed it for all but one program.
The first program which is to close automatically after manually closing the second, doesn't close because of lack of "permission".  I tried giving the exe, and the shortcut and even the folder the program is in administrative privledges and permissions but it still doesn't close.  =(

I've tried lot's of things.  If you could assist with this I would greatly appreciate it.  Much thanks!
Title: Re: Need .bat to launch 2 programs. P1 exits after P2 is closed.
Post by: patio on March 05, 2018, 07:28:01 PM
Might help a bit to post what app it is...
Title: Re: Need .bat to launch 2 programs. P1 exits after P2 is closed.
Post by: BC_Programmer on March 05, 2018, 09:49:21 PM
The batch file/command prompt wouldn't have permission to kill the task if one of the programs elevates and runs as admin, either itself or through compatibility settings.

In any case I came up with this version:

Code: [Select]
@echo off
set prog1=notepad.exe
set prog2=mspaint.exe
for %%P in (%prog1%) do set prog1name=%%~nP
start %prog1%
start /wait %prog2%
tskill %prog1name%

(Of course notepad.exe and mspaint.exe are placeholders!). I think start /wait returns immediately sooner if the program self-elevates, as that typically involves relaunching a new instance of the program and exiting the existing one, s o this could have issues there.
Title: Re: Need .bat to launch 2 programs. P1 exits after P2 is closed.
Post by: nil on March 06, 2018, 10:12:52 AM
You might also try running the controlling batch file in an elevated command prompt, e.g. from Start Menu -> expand Windows System -> right-click Command Prompt -> More -> Run as Administrator.