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 fou开发者_开发问答nd 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
?
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".
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.
精彩评论