问题
I am trying to get a song to play in the background of Windows, after a little looking I found this:
@echo off
set file=RRLJ.mp3
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
echo Sound.URL = "%file%"
echo Sound.Controls.play
echo do while Sound.currentmedia.duration = 0
echo wscript.sleep 100
echo loop
echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000) >sound.vbs
start /min sound.vbs
This works well for starting the song but I have no way of stopping it from a .bat
file. The only way I found to cut it short is to open task manager and close it from the processes.
I have tried:
taskkill /im wscript.exe
But I keep getting something in the window saying: Success: Sent termination sidnal to the process "wscript.exe" with PID 185448 but the music continues to play until I manually end it with task manager
回答1:
I'd use /T
switch (Tree kill): terminates the specified process and any child processes which were started by it.
Here is a script to find ProcessID
to terminate exactly needed process only using PID
:
@ECHO OFF >NUL
for /F "usebackq tokens=*" %%G in (
`wmic process where "CommandLine like '%%sound.vbs%%' AND Caption like '%%script.exe%%'" get ProcessID/value ^|find /I "="`
) do (
rem echo %%G
for /F "tokens=2 delims==" %%H in ("%%~G") do echo taskkill /T /F /PID %%H
)
Note taskkill
command is echoed merely... Remove echo
when debugged.
来源:https://stackoverflow.com/questions/29270599/stop-music-once-playing