Why cant i use wildcards in TASKLIST /FI "WINDOWTITLE" function/filter?

Antony Buck picture Antony Buck · Feb 6, 2012 · Viewed 18.7k times · Source

first post.

I am writing a batch file to check the windowtitle of open windows, and close them and reopen them if they dont match what i want them to. (main reason is to auto restart browser if the 'page cannot be loaded').

My problem lies here...

tasklist /FI "WINDOWTITLE eq Kingdoms of Camelot on Facebook - Mozilla Firefox"2>NUL | find /I /N "firefox.exe"2>NUL
    if "%ERRORLEVEL%"=="0" goto runningfire
    if "%ERRORLEVEL%"=="1" goto notrunningfire

This checks open windows and compares their title to 'Kingdoms of Camelot on Facebook - Mozilla Firefox". This works great, UNTIL!!!

The major annoyance for me is whenever you get a facebook notification/friend request/message, it changes the title bar to (1) Kingdoms..... and then the script doesnt work.

I have spent AGES on this, mainly because i am VERY much novice at this, and i know very little about batch files and script.

So my question is this...

Is it possible to effectly add wildcards to the start of the title requirements...(note the * at front of title)

tasklist /FI "WINDOWTITLE eq *Kingdoms of Camelot on Facebook - Mozilla Firefox"2>NUL | find /I /N "firefox.exe"2>NUL | find /I "[6]"2>NUL
    if "%ERRORLEVEL%"=="0" goto runningfire
    if "%ERRORLEVEL%"=="1" goto notrunningfire

This was the obvious solution, but it doesnt work at all, returns ERROR: The search filter cannot be recognized.

I also tried 3 ??? to represent (1) and (2) etc, but they wont work either....

Any ideas? Please aim it at a complete and utter noob.

Answer

MrJames picture MrJames · Mar 30, 2012

Why don't you try

tasklist /fi "imagename eq firefox.exe" /v | find /i "Kingdoms of Camelot on Facebook - Mozilla Firefox" 2>NUL
if ....

I'm using the same approach to get the pid's of the processes that have the same window title prefix and it works.

PS: From what I've tested of tasklist command it's doesn't recognized wildcard syntax