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

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.


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.