Page 1 of 1

Batch script execution controller

Posted: 27 Jul 2021 15:21
by MPDEV
Hi everyone and thanks in advance for the time dedicated :)
I'm trying to write a batch script that, in sequence:
1) Start an executable (which in this case is "new_emulator.bat", but it is only for example)
2) Controls repeatedly if the process generated from point 1 is still running.
My problem occurs precisely in the control phase, in the only IF present throughout the script (below), because it does not follow the instructions that should perform if the control had successful results, nor what should be performed otherwise ( That is to go back and create another process of the same executable). Please help me, I don't know what to do anymore!

THE SCRIPT:

Code: Select all

set /A i=0
:infinite_loop
set /A i=%i%+1
set windowTitle=singularBot%i%
START "%windowTitle%" C:\Users\Administrator\Desktop\Nuovo_emulatore.bat
setlocal enableextensions 
for /f "tokens=*" %%a in ( 'tasklist /FI "WindowTitle eq %windowTitle%*" /NH' ) do ( set riga=%%a ) 
set riga=%riga:~0,42%
goto:anomal_closing_check

:anomal_closing_check
for /f "tokens=*" %%a in ( 'tasklist /FI "WindowTitle eq %windowTitle%*" /NH' ) do ( set actual_output=%%a ) 
set actual_output=%actual_output:~0,42%
timeout /t 4 /nobreak
IF [%riga%] EQU [%actual_output%] (goto stillOpen) ELSE (goto closed)

:stillOpen
timeout /t 4 /nobreak
goto anomal_closing_check

:closed
goto infinite_loop

Re: Batch script execution controller

Posted: 28 Jul 2021 07:40
by Squashman
I am not sure why you are checking if the process is still running. You are not doing anything with that information other than just checking it is running and if it is no longer running then restart the process again.
Why couldn't your script be as simple as this?

Code: Select all

set /A i=0
:infinite_loop
set /A i+=1
set windowTitle=singularBot%i%
TITLE singularBot%i%
CALL "C:\Users\Administrator\Desktop\Nuovo_emulatore.bat"
goto infinite_loop