-1

I am using a batch file to run a program, then i leave the console open while the program runs. I am looking for a way to call another batch file from the same script, triggered by closing of the program i opened before. How can i do that?

Mateusz C
  • 25
  • 3
  • Your best option will be if the app itself logs an entry in the event log on closing. Then you can trigger an SCHTASK based on this event. – npocmaka Jan 26 '18 at 11:11
  • yes it can be done when running the batch with a wait option, if it then terminates, run the next command in the same batch. – Gerhard Jan 26 '18 at 11:14
  • very much depends on your program. Calling it with a batchfile (`start /wait program.exe`) might wait or might not wait. before the batchfile continues with the next command. Try it and report failure/success. – Stephan Jan 26 '18 at 11:14
  • ok will try those, thank you – Mateusz C Jan 26 '18 at 11:16
  • @Stephan Yup, the method with /wait worked well. On what depends whether this method will work, on a program or a system which it is being run on. I'd like it to be as universal as possible. – Mateusz C Jan 26 '18 at 11:27
  • About schtask method, i have to put some research into it and will do that later – Mateusz C Jan 26 '18 at 11:28
  • whether `start /wait` works, depends on how the application is programmed. It's nothing you have any influence on (execpt you are very good at hacking). Same with the entry in the event log (suggested by npocmaka): you'd have to change the code of the program. The `tasklist` approach should be quite universal. – Stephan Jan 26 '18 at 11:33

1 Answers1

1

You could use: START /WAIT program.exe

Bare in mind that this won't work when the program you are trying to start is a launcher as the launcher could close itself after starting the actual program or stay open although the actual program was closed; thus the next line would be executed when the launcher is closed.

If this should be the case you might be able to make use of TASKLIST (Source):

:check_status
TASKLIST /FI "IMAGENAME EQ program.exe" |FIND ":" > nul
IF ERRORLEVEL 1 GOTO check_status

You can also add TIMEOUT if you don't need the next line to be execute immediately which will significantly decrease CPU usage:

:check_status
TASKLIST /FI "IMAGENAME EQ program.exe" |FIND ":" > nul
IF ERRORLEVEL 1 TIMEOUT /T 1 /NOBREAK && GOTO check_status
FatalBulletHit
  • 563
  • 3
  • 20