11

The start command can launch an application like notepad in a batch file like this:

start notepad
start "my love.mp3"

But how do I close the running application from the command line? I found taskkill in my searches but I don't think that is the right command because it's not working—it says no such file.

How do I close an application launched with start?

ErikE
  • 48,881
  • 23
  • 151
  • 196
niko
  • 9,285
  • 27
  • 84
  • 131

2 Answers2

18

Enter taskkill /? for the syntax and some examples. What you want to do is pass the /IM argument using the name of the program you want to kill. For example:

TASKKILL /F /IM notepad.exe

will kill notepad.exe.

TASKKILL /F /IM note*

will kill all processes beginning with "note".

Patrick Cuff
  • 28,540
  • 12
  • 67
  • 94
5

Taskkill is correct. But you must kill the process playing the file, not the file itself. Figuring out the registered handler for mp3 files from a command prompt will be a bit tricky.

If you know it then you can kill that process.

Here's a script that figures out the registered application for mp3 files and kills the task:

@echo off
if not .%1==. goto show

:createtemp
set tempfile="%temp%\temp-%random%-%time:~6,5%.bat"
if exist %tempfile% goto :createtemp

reg query HKEY_CLASSES_ROOT\mp3file\shell\play\command\ > %tempfile%

for /F "skip=4 delims=> tokens=2 usebackq" %%e in (`type %tempfile%`) do call %0 %%e

del %tempfile% > nul
set tempfile=
set handler=
set teststring=
set offset=
set cmd=
goto end

:show
set handler=%2
set handler=%handler:~1,-1%
set /A offset=-1

:loop
set cmd=set teststring=%%handler:~%offset%%%
echo %cmd% > %tempfile%
call %tempfile%
if %teststring:~0,1%==\ goto complete
set /A offset=offset-1
goto loop

:complete
set /A offset=offset+1
set cmd=set handler=%%handler:~%offset%%%
echo %cmd% > %tempfile%
call %tempfile%
taskkill /IM %handler% > nul

:end

If you save this as killmp3.bat or something, you can call it when you want. Of course be aware that if the program was already running, doing something else, that it will be closed anyway.

Note that this depends heavily on the entry in the registry to have the executable path inside double quotes. If you don't have that and there are spaces in the executable name, it will fail.

You could generalize my technique to be able to pass in the file extension (such as .mp3, which you could look up in the registry to find the class name mp3file and then find the handler from there.

A more generic solution that takes the name of the file you started and figures out the extension from it is theoretically possible but a lot more difficult. In the case of notepad you have to figure out what that is by searching the path for all executable files, etc.

This might be simpler if you created an extremely short mp3 file that you could start. Depending on the program, it might stop playing the current file and switch to the new one, which would end almost instantly and effectively stop playback.

ErikE
  • 48,881
  • 23
  • 151
  • 196
  • Thank you.But how do i find out Can you give me any sample batch file that does the operation ? – niko Jul 15 '11 at 05:08
  • please let me know how to kill a application say a notepad using the taskkill.How do i find a registered handler ? Any command to do that – niko Jul 15 '11 at 05:09
  • Erike can you help me something.In my pc dos command it says taskkill command is not recognized how do i update commands in dos? any idea – niko Jul 15 '11 at 17:34
  • @Niko see edits again. I commented on your question to ask what version of Windows you're running. – ErikE Jul 15 '11 at 19:41