taskkill - end tasks with window titles ending with a specific string

5

1

I need to write a batch program to end all MS office communicator tasks with window titles (usually ending with pattern "- Conversation" .

I tried

taskkill /FI "WINDOWTITLE eq *Conversation" /IM communicator.exe

but the wildcard pattern starting with a '*' does not seem to work. Gives the folowing error

ERROR: The search filter cannot be recognized.

any suggestions for a workaround would be greatly appreciated!

dbza

Posted 2012-12-10T17:03:23.480

Reputation: 508

1'*' Can't work in the beginning of the filtered text – Ofiris – 2012-12-10T17:18:44.567

Answers

2

Here’s a batch file partially inspired by this question by Rogier:

@echo off

for /F "delims=" %%a in ('tasklist /fo list /v') do (
      call :Sub %%a
)
exit /b
 

:Sub
set Line=%*
set BOL4=%Line:~0,4%
set BOL13=%Line:~0,13%
set Value=%Line:~14%
if "%BOL4%"=="PID:" (
      set save_PID=%Value%
      exit /b
)
if "%BOL13%"=="Window Title:" (                          // Note . below.
      echo %Value% | findstr /r /c:"- Conversation.$" > nul
      if not errorlevel 1 (
            echo %save_PID%
            REM taskkill /pid %save_PID%
      )
      exit /b
)
exit /b

I found that I needed to add a . at the end of the regular expression for the

echostring| findstr /rregular expression ending with $

form to work.  I’m guessing that echo is adding a CR or maybe an extra CRLF to the string, and that findstr is counting that as a character occurring between the string and the end of the line.

Obviously you will un-comment-out the taskkill command one you have tested this.

Scott

Posted 2012-12-10T17:03:23.480

Reputation: 17 653

3

I do appreciate the Batch file answer that uses a FOR command.

Here is another method, presented here simply as an additional option. The approach is rather different than the other answer that is provided.

First, see if you can find the MS Communicator processes with WMIC. e.g.:

WMIC PROCESS LIST FULL

Or, to show information horizontally in a table instead of vertically in Property=Value format, do:

WMIC PROCESS LIST FULL /FORMAT:TABLE

or perhaps, to limit that down:

WMIC PROCESS GET NAME /FORMAT:TABLE

(and ignore the first line)

This doesn't seem to show Window title, but may show lots of other bits of information that may be used to uniquely identify Communicator instances, like Executable names.

Then, you can start getting more aggressive, narrowing down your findings. The following looks for instances that end with "name.exe" (e.g., "filename.exe", "myname.exe")

WMIC PROCESS WHERE "Description like '%%name.exe'" LIST FULL /FORMAT:TABLE

All that was just research to fine-tune your command. (After all, you don't really want to start terminating the wrong processes.)

Finally, once all your research is done, so you know you can list just the stuff you want to terminate, then start killing.

WMIC PROCESS WHERE "Description like '%%name.exe'" DELETE

The really nice part about this method is you quickly find ways to be able to gather lots of information that can be used for automation.

TOOGAM

Posted 2012-12-10T17:03:23.480

Reputation: 12 651

This did the trick for me. – John Livermore – 2018-03-17T05:08:44.973