1
votes

I have a batch file script which starts a program (bat) and after X seconds the batch script is close the program (exe) and restart it.

This is my code:

:loop
start "1.bat" "C:\1\1.bat"
Timeout 10
taskkill /f /im program.exe
goto loop

If the program is ending, I want that the Cmd.exe window closes, but only the "1.bat" window should be closed. I can't find a specified cmd window for my "1.bat" process in the task manager to close it.

If I close the programm.exe with && exit the batch doesn't restart!

1
What is in 1.bat? Why can you not add exit at the end of 1.bat or use another TIMEOUT? - FatalBulletHit

1 Answers

0
votes

You can do one of two things.

1 - If you have access to 1.bat (i.e. its not readonly or is not actually an .exe), then add exit after it runs the program.exe.

2 - Otherwise if you cannot modify 1.bat for whatever reason, you may need to add the following command line:

taskkill /FI "IMAGENAME eq cmd.exe" /FI "WINDOWTITLE eq 1.bat"

So your batch script will look as follows:

:loop
start "1.bat" "C:\1\1.bat"
Timeout 10
taskkill /f /im program.exe
taskkill /FI "IMAGENAME eq cmd.exe" /FI "WINDOWTITLE eq 1.bat"
goto loop

Or similar. Basically /FI is the filter switch and you can specify to filter based on the "IMAGENAME" (which equals "cmd.exe") and "WINDOWTITLE" (which equals "1.bat")