Windows批处理文件 - 如果TASKKILL窗口标题包含文本 [英] Windows Batch file - taskkill if window title contains text
问题描述
我想要写一个简单的批处理文件来杀死包含窗口标题的某些文本的过程。现在我有:
I want to write a simple batch file to kill a process that contains certain text in the window title. Right now I have:
taskkill /fi "Windowtitle eq XXXX*" /im cmd.exe
和的作品,但我想要做的是无论是在标题的开头和结尾使用通配符。因此,像:
And that works, except what I want to do is use the wildcard both at the beginning AND end of the title. So something like:
taskkill /fi "Windowtitle eq \*X*" /im cmd.exe
但我尝试这样做,这是行不通的。是否有什么我失踪,或者这是不可能的?
But I tried this and it does not work. Is there something I'm missing or is this not possible?
推荐答案
没有,通配符不会在过滤器开始允许的。
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
这将检索任务列表,在CSV和详细格式(包括窗口标题作为输出的最后一个字段)。
This will retrieve the list of tasks, in csv and verbose format (that will include window title as the last field in the output).
是 FINDSTR
过滤,用常规的前pression,将搜索指定的文本( X $ C $名单C>)的最后一个字段。
The list is filtered by findstr
with a regular expression that will search the indicated text (the X
) in the last field.
如果任何线路滤波器,匹配为
将记号化它,检索将在TASKKILL用来结束过程中的第二个字段(PID)。
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.
这篇关于Windows批处理文件 - 如果TASKKILL窗口标题包含文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!