4

When I want to close the following two (fictional...) applications using taskkill...

Two hello-sayers

...I would use taskkill /FI "WINDOWTITLE eq Hello*".

But how about these two:

Alcoholic programs

taskkill /FI "WINDOWTITLE eq * wine" gives me FEHLER: Der Suchfilter wurde nicht erkannt., which can be translated as ERROR: The search filter could not be recognized.

So, how can I filter with a wildcard at the beginning?

Bowi
  • 1,378
  • 19
  • 33

1 Answers1

4

The wildcard at the beginning does not work. You would need to incorporate findstr using a bit of initiative.

for /f "tokens=2 delims=," %%a in ('tasklist /fi "imagename eq notepad.exe" /v /fo:csv /nh ^| findstr /r "wine"') do taskkill /pid %%a

So we search for imagenames with wine in the name. Use /fo to csv format, /nh for no header, then search for the string "wine" in imagename, then kill by process ID if found.

To not be imagename specific do:

for /f "tokens=2 delims=," %%a in ('tasklist /v /fo:csv /nh ^| findstr /r "wine"') do taskkill /pid %%a

Edit

As for the concern in killing incorrect tasks:

@echo off
set "images=notepad.exe,calc.exe,winword.exe,excel.exe"
for %%i in (%images%) do (
   for /f "tokens=2 delims=," %%a in ('tasklist /fi "imagename eq %%i" /v /fo:csv /nh ^| findstr /r "wine"') do taskkill /pid %%a
)

Just add a list of possible image names that would contain the title, it will only loop these as per below and not touch the other processes/tasks:

tasklist /fi "imagename eq notepad.exe"
tasklist /fi "imagename eq calc.exe"
tasklist /fi "imagename eq winword.exe"
tasklist /fi "imagename eq excel.exe"
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Wait, the *imagename* you searched for is `notepad.exe` – I haven't stated it explicitly (but hinted it by using a notepad and a calculator), but the windows do not have to have the same imagename/executable. – Bowi Sep 06 '18 at 15:06
  • @Bowi You do not have to specify /im at all but it will run slightly longer as it will findstr each task. See edit. – Gerhard Sep 06 '18 at 15:07
  • That makes the answer perfect. :-) – Bowi Sep 06 '18 at 15:09
  • 1
    @Bowi, Be aware that with this solution, the search is not limited to window titles, it will search through all fields that is returned by `tasklist`, including Process Name, Session Name, ... so for example if your search term matches a process name you will kill that process too, even it has a different window title. Another dangerous example is if you replace `wine` by `Console` or even `sol`, all processes in your session which have a window (`taskkill`) or all processes (`taskkill /f`) may/will be killed. So It is better use the `/IM` switch, to limit the scope of probable unwanted kills. – sst Sep 06 '18 at 23:20
  • @Bowi, I would suggest you to not try the search terms `Console` or `sol` or any substring of *Console*, If you want to test, replace `taskkill /pid %%a` by `echo %%a` to see list of process ids that `taskill` would attempt to kill. – sst Sep 06 '18 at 23:31
  • @sst, that is a very good point, thank you! So the solution would clearly be to restrict the imagename and fire multiple taskkill commands, one for each imagename? One could do this with a loop (or simply c&p). – Bowi Sep 07 '18 at 07:36
  • 1
    @bowi, one way is to list all the possible imagenames that could contain the title in a file or in the script itself and loop through it, if the imagename is not listed, it will not be killed. – Gerhard Sep 07 '18 at 07:37
  • I find it very elegant -- if you give the IM to tasklist, you speed up the findstr. – Bowi Sep 07 '18 at 07:51
  • 1
    Correct, hence why I mentioned yesterday that removing IM will become slow as it runs through all processes. Enjoy :) – Gerhard Sep 07 '18 at 07:52