Windows Batch file - taskkill if window title contains text

笑着哭i 提交于 2019-11-30 12:11:59

No, wildcards are not allowed at the start of the filter.

for /f "tokens=2 delims=," %%a in ('
    tasklist /fi "imagename eq cmd.exe" /v /fo:csv /nh 
    ^| findstr /r /c:".*X[^,]*$"
') do taskkill /pid %%a

This will retrieve the list of tasks, in csv and verbose format (that will include window title as the last field in the output).

The list is filtered by findstr with a regular expression that will search the indicated text (the X) in the last field.

If any line matches the filter, the for will tokenize it, retrieving the second field (the PID) that will be used in taskkill to end the process.

In the special case you have started the command window from a batch file yourself, you can specify the window title using the command

START MyWindowTitle c:/MyProcess.exe 

That way it is easy to kill the process again using just

taskkill /fi "WindowTitle eq MyWindowTitle"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!