1

I have 2 programs, A and B. I made a batch file to have them open simultaneously, and now I want to have them both close when I exit program A.

Here's my code so far:

@echo off

cd "C:\prog\test"
start A.exe

cd "C:\prog\test"
start B.exe

exit

I can't find any tutorial that doesn't involve some kind of timer. Problem is, I have no idea how long I'll be working with them before closing. Can anyone help?

nguyen long
  • 83
  • 1
  • 11

3 Answers3

1

Something along this line.

@echo off
START
cd "C:\prog\test\"
startA.exe && startB.exe

:TEST
tasklist /FI "IMAGENAME eq startA.exe" 2>NUL | find /I /N "startA.exe">NUL 
if "%ERRORLEVEL%"=="0" goto ACTIVE

:DEAD
taskkill /f /im startB.exe
exit

:ACTIVE
timeout /T 10
goto TEST

So to explain.

You start your programs, then test for startA.exe using tasklist. If the task returns Errorlevel==0 it means it is running. We then send to the waiting section for 10 seconds, and go back to test if startA.exe is running. If however the errorlevel is not 0, it will go directly to DEAD and kill startB.exe

Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

Looks like here: Batch script - Run exe program one after another

And here: Parallel execution of shell processes

Is solution that you need.

0

Here's an untested example.

It works by accepting the name of the game iso as an input parameter, e.g. "C:\MyGames\Game1\game1.iso", (could be dragged and dropped onto the script).

@Echo Off
Set "VDX=%ProgramFiles%\RES Software\VDX Engine\VDX_x64.exe"
Set "EMU=%SystemDrive%\Emulators\Sony\pcsx2-v1.3.1\"
Set "EXE=pcsx2.exe"

Start "" "%VDX%"
Start "" "%EMU%%EXE%" --nogui --fullscreen "%~1" 

:Loop
Timeout 30 /NoBreak>Nul
QProcess|FindStr/IRC:"\<%EXE%\>">Nul&&(GoTo Loop
)||TaskKill/T /F /IM "%EXE%">Nul

Please feel free to alter the paths as necessary in the Set statements, (making sure to leave the closing doublequote).

I decided to perform the check every 30 seconds, if you wish to change that then replace 30 in Timeout 30 to your preference. If you are using Windows XP which doesn't have Timeout replace Timeout 30 with Ping -n 30 0.0.0.0>Nul

You may also optionally or if necessary replace QProcess with TaskList/NH /FI "IMAGENAME eq %EXE%"

Compo
  • 36,585
  • 5
  • 27
  • 39
  • @nguyen, please consider revising your currently accepted answer fo my more in depth response if you feel it better achieved your intent; Thank you. – Compo Oct 25 '17 at 18:58