Question

OS: Windows XP, Windows 7 64bit.

We have some fairly hefty cmd scripts that are used for some daily build processes. These scripts spawn numerous other (windowed) processes. There is one controlling cmd script, a small simple script, which starts the main cmd script. The purpose of the small controlling script is to clean up in situations where the main script or any of its children fail. This is accomplished fairly easily: the main script and all its children have window titles which begin with a unique identifier. When the controlling script determines that the main script and all its children should have completed, it uses tasklist to find windows of any hung processes, via:

tasklist.exe /FI "WINDOWTITLE eq UniqueIdentifier*"

This all worked very nicely in XP. Now enter Windows7 64-bit. Here, if the main .cmd script or any other .cmd shell window attempts to sets its window title via

title UniqueIdentifier Followed By Descriptive Text

Windows7 64-bit kindly prepends other text to the title (specifically, "Administrator: " or similar). The prepended text cannot be relied upon. So now we want to use

tasklist.exe /FI "WINDOWTITLE eq *UniqueIdentifier*"

but THIS FAILS with the error message "The search filter cannot be recognized". Going the route of using our UniqueIdentifier as a post-fix does not work: the command

tasklist.exe /FI "WINDOWTITLE eq *UniqueIdentifier"

also results in the same error message. It seems that Microsoft's notion of "wildcard" in a filter does not extend beyond having a "*" as the terminal character. Ouch.

DOES ANYONE HAVE ANY WORK-AROUNDS? Pslist does not seem to allow filtering with window title.

Was it helpful?

Solution

You can use the /V option to include the window title in the output and then pipe the result to FIND (or FINDSTR) to filter the result.

tasklist /v | find "UniqueIdentifier"
tasklist /v | findstr /c:"UniqueIdentifier"

If using FINDSTR then I recommend using the /C option so that you can include spaces in the search string.

You might want to use the /I option if you need to do a case insensitive search.

OTHER TIPS

Based on my experimentation, the wildcard for taskkill only seems to work at the end of a string, not in any other position. I can't find any documentation from Microsoft one way or another about this. However all of the examples in the documentation follow this format

Successful:  notepad*
Fails: notepad*.exe
Fails *notepad*

As mentioned in the other answer, best to parse the output of tasklist to get exactly what you want rather than relying on the rather broken behavior of taskkill.

Yes, it works fine if the * is in the end of the process name searched for, as pointed out by fiver.
Here is an example on how to run the command:

tasklist /FI "IMAGENAME eq no*"

I think this works on Windows 10. Here's my snippet

set PROCNAME="Foobar"
tasklist /FI "IMAGENAME eq %PROCNAME%*" 2>NUL | find /I /N %PROCNAME%>NUL
if "%ERRORLEVEL%"=="0" (
    echo it is running
)

Notice the asterisk in the filter.

Use powershell. Get-Process. way more useful if you can handle the power that is. Try

get-process | where MainWindowTitle -like "*UniqueIdentifier*" | select *
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top