Executing a command when closing Batch file

253 views Asked by At

I have a batch file playing a sound in the background while it runs, using the method at Batch file executing a sound silently however, when I exit the batch file, the music will continue to play.

Id there any way to run a command while a batch file is closing, so that the sound file will stop playing?

I've tried an example I found called onexit:

onexit taskkill /im wmplayer.exe

But it doesn't work.

2

There are 2 answers

0
SachaDee On

Just :

taskkill /f /im wmplayer.exe

Should work.

5
BaBa On

First idea i had is another batch:

@ECHO OFF
START "" /W <YourBatchhere>
TASKKILL /im "wmplayer.exe"

should do the job :)

The /W will wait at that point, still YourBatchhere has ended. Regardless of success or error.