I wrote a script(working) which kills every task which is not equal with the windowtitle DEFENDINGSOFTWARE.
So it is:
Taskkill /FI “WINDOWTITLE ne DEFENDINGSOFTWARE*“ /F
How can I manage that he is also not killing the tasks which Name is “DEFENDINGSOFTWARE1”?
The * already takes care of that.
Taskkill /FI "WINDOWTITLE ne DEFENDINGSOFTWARE*" /F
should kill
DEFENDINGSOTWARE
DEFENDINGSOTWARE1
DEFENDINGSOTWARE2
DEFENDINGSOTWARE - some text
As an example do from cmdline:
start "Test" pause
start "Test2" pause
You will have 2 titled cmd windows called
Test - pause
Test2 - pause
now run
taskkill /FI "WINDOWTITLE EQ Test - pause"
which will only kill Test - pause
but running:
taskkill /FI "WINDOWTITLE EQ Test*"
Kills all windows with a title starting with Test
Also, by running taskkill /? from cmdline you will see the filter switch help.
/FI filter Applies a filter to select a set of tasks.
Allows "*" to be used. ex. imagename eq acme*
with examples further down, where I selected the most relevant example, demonstrating this behaviour.
TASKKILL /F /FI "PID ge 1000" /FI "WINDOWTITLE ne untitle*"
Related
So I want to close my live wallpaper when epic games launcher starts and then start it when i close the launcher and with other programs it worked fine but now it gives me this error message:ERROR: Invalid argument/option - 'Live'.
I think this is because the live wallpaper has spaces in its name, but I tried quotation marks and it still doesn't work. Do you know what I should do?
#echo off
:TEST
tasklist /nh /fi "imagename eq EpicGamesLauncher.exe" | find /i "EpicGamesLauncher.exe" >nul
if "%ERRORLEVEL%"=="1" goto TIMER
taskkill /im "DesktopHut Live v5.0.0.exe" /t /f
:TIMER
timeout /T 10
goto TEST2
:TEST2
tasklist /FI "IMAGENAME eq EpicGamesLauncher.exe" 2>NUL | find /I /N "EpicGamesLauncher.exe">NUL
if "%ERRORLEVEL%"=="0" goto TEST
tasklist /nh /fi "imagename eq "DesktopHut Live v5.0.0.exe" "| find /i /n "DesktopHut Live v5.0.0.exe" >nul
if "%ERRORLEVEL%"=="0" goto TEST
start " " "C:\Users\David\Desktop\DesktopHut Live v5.0.0.exe"
goto TEST
pause>nul
The reason for the error message is that you've used a wrong syntax for the filter expression of tasklist: tasklist /nh /fi "imagename eq "DesktopHut Live v5.0.0.exe" "
The filter expressions can have spaces in them and tasklist does not have any problems with them, The real problem is the limitation in the output of tasklist which is apparently fixed at 25 characters for the Image Name column, when using the default output formatting.
So assuming the process DesktopHut Live v5.0.0.exe is already running, The output of
tasklist /fi "imagename eq DesktopHut Live v5.0.0.exe"
would be:
Image Name
=========================
DesktopHut Live v5.0.0.ex
To overcome that limitation you can specify csv as the output format by the /fo csv switch
tasklist /fi "imagename eq DesktopHut Live v5.0.0.exe" /fo csv | find /i "DesktopHut Live v5.0.0.exe" >nul
Since you are using find to set the errorlevel, there is no need to use a filter expression for tasklist so the entire command can be simplified to:
tasklist /fo csv | find /i "DesktopHut Live v5.0.0.exe" >nul
I want to write a simple batch file to kill a process that contains certain text in the window title. Right now I have:
taskkill /fi "Windowtitle eq XXXX*" /im cmd.exe
And that works, except what I want to do is use the wildcard both at the beginning AND end of the title. So something like:
taskkill /fi "Windowtitle eq \*X*" /im cmd.exe
But I tried this and it does not work. Is there something I'm missing or is this not possible?
No, wildcards are not allowed at the start of the filter.
for /f "tokens=2 delims=," %%a in ('
tasklist /fi "imagename eq cmd.exe" /v /fo:csv /nh
^| findstr /r /c:".*X[^,]*$"
') do taskkill /pid %%a
This will retrieve the list of tasks, in csv and verbose format (that will include window title as the last field in the output).
The list is filtered by findstr with a regular expression that will search the indicated text (the X) in the last field.
If any line matches the filter, the for will tokenize it, retrieving the second field (the PID) that will be used in taskkill to end the process.
In the special case you have started the command window from a batch file yourself, you can specify the window title using the command
START MyWindowTitle c:/MyProcess.exe
That way it is easy to kill the process again using just
taskkill /fi "WindowTitle eq MyWindowTitle"
A little more elaborate, but you can use:
for /f "tokens=2 delims== " %%A in ('tasklist /nh /fi "imagename eq cmd.exe" /fi "windowtitle eq MyWindowTi*"') do set "PID=%%A"
taskkill /F /T /PID !PID!
I am in need of help to adding a not responding check to my .bat and restarting a server application when it is not responding. This is what I have so far.
#echo on
tasklist /nh /fi "imagename eq javaw.exe" | find /i "javaw.exe" >nul && (
echo Server is running.
//
I need a not responding check right afterward here. If the program is not responding,
taskkill /f /IM server.exe
taskkill /f /IM javaw.exe
cd C:\Users\Administrator\Desktop
serverstart.jar
else
exit
//
exit
) || (
echo Server process not found.
taskkill /f /IM server.exe
taskkill /f /IM javaw.exe
cd C:\Users\Administrator\Desktop
serverstart.jar
)
pause>nul
It is necessary for me to kill both of these process because it doesn't end eachother when it crashes apparently.
You already managed to filter by imagename.
You can also filter by Status (Running / Not Responding / Unknown):
tasklist /nh /fi "imagename eq java.exe" /fi "status eq not responding"
will give you any java.exe, which does not respond.
EDIT
well - when I think about it: I would try:
tasklist /nh /fi "imagename eq javaw.exe" /fi "status eq running` |find /i "javaw.exe" >nul && (
echo Server is running
rem nothing to do
) || (
echo Server is not running or not responding
rem restart service
)
I try to close all firefox if there are more than one. I want to do loop that check the firefox process and close and check again until there is no any firefox process.
This is my code:
:loop
taskkill /im "firefox.exe"
tasklist /fi "imagename eq firefox.exe" goto loop
Where is my wrong in this command ? Any help is appreciated.
:loop
tasklist /fi "imagename eq firefox.exe" | find "firefox.exe" >nul && ( taskkill /im firefox.exe >nul & goto loop )
Get list of tasks, and if firefox.exe included in it, then kill it and goto loop
You can also use the following to force kill FF windows:
taskkill /F /FI "ImageName eq firefox.exe"
One taskkill command will kill every firefox process - unless it is not responding.
You could use two taskkill commands separated by a delay of several seconds, and the second one should use the /f force switch.
I do
#echo off
TASKKILL /IM firefox.exe /F
pause>nul
exit
I'm on windows 7, it works for me
I am executing following command in a label inside a batch file:
tasklist.exe /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2" 2>nul && echo errorl:%errorlevel%
%1 is process running and %2 is its PID.
Even if process and its PID matches or doesnt matches, I m getting "errorl:1" in o/p.
I am not sure whats wrong here. Any idea?
You could pipe tasklist through the find command and get an errorlevel off of it.
Example:
tasklist | find "firefox.exe"
echo Error level = %ERRORLEVEL%
REM If firefox is running, the errorlevel is set to 0
REM If firefox is not running, errorlevel is set to 1
In my opinion, you can't use errorlevel at all,
because tasklist always returns a 0 even if the pid isn't found.
I suppose, you have to parse the output of tasklist.
To fetch the output of a command, FOR /F is a good choice.
To avoid problems wth the quoting in the FOR /F, I build first a cmd variable which is expanded with delayed expansion to avoid any side effects of special characters.
#echo off
setlocal enableDelayedExpansion
set "cmd=tasklist.exe /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2""
for /F "delims=*" %%p in ('!cmd! ^| findstr "%2" ') do (
echo found %%p
)
%variables% are expanded before executing the line, so %errorlevel% will expand to some old value. (The fact that the code after && executes at all is your clue that the command returned 0)
You options are:
Use %errorlevel% or the more correct IF errorlevel 1 ... on the next line
Call setlocal ENABLEDELAYEDEXPANSION first and then use !errorlevel!
Edit:
I guess tasklist is buggy and/or stupid when it comes to exit codes, I wrote some code that does not use the exit code at all:
#echo off
if "%~1"=="SOTEST" (
start calc
ping -n 2 localhost >nul
for /F "tokens=1,2 skip=3" %%A in ('tasklist /FI "IMAGENAME eq calc.exe"') do (
call "%~0" %%A %%B
)
call "%~0" dummy.exe 666
goto :EOF
)
goto main
:IsTaskRunning
setlocal ENABLEEXTENSIONS&set _r=0
>nul 2>&1 (for /F "tokens=1,2" %%A in ('tasklist /FO LIST %*') do (
if /I "%%~A"=="PID:" set _r=1
))
endlocal&set IsTaskRunning=%_r%&goto :EOF
:main
call :IsTaskRunning /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2"
if %IsTaskRunning% gtr 0 (echo.%1:%2 is running) else (echo.%1:%2 is NOT running)
Run it as test.cmd SOTEST and it prints:
calc.exe:4852 is running
dummy.exe:666 is NOT running
Easy solution to this, given that
1) you can't get an errorlevel from tasklist, and
2) you can't directly pipe it to a FIND
Just write it to a file using output redirection and use FIND to check the file. Each time this is run, it will overwrite the previous iteration, so no need to even do any file cleanup. Amazing how many bat/cmd file limitations can be overcome with a simple scratchpad file!!
:TOP
rem swap rems from good to bad to test
set findvar=goodfile.exe
rem set findvar=badfile.exe
set scratchfile=scratch.txt
tasklist /fi "status eq running" /fi "imagename eq %findvar%">%scratchfile%
type %scratchfile%
pause
echo Looking for %findvar%
find "%findvar%" %scratchfile%
echo Error level = %errorlevel%
pause
IF errorlevel 1 GOTO BAD
IF errorlevel 0 GOTO GOOD
GOTO OTHER
:BAD
echo Errrlevel 1 - task not found
PAUSE
GOTO TOP
:GOOD
echo Errrlevel 0 - task is running
pause
GOTO TOP
:OTHER
echo something else ????? you blew it somewhere!
tasklist returns 0 when executes successfully:
If you're looking for existence of some process or some attribute of a process, one method is to supply the attributes to tasklist and check if it returned any process names. If no matching processes are found, it'll return "INFO: No tasks are running which match the specified criteria."
The result of tasklist may be checked either via for command embedding (and parse command output) or filter via find or findstr, which accepts regular expressions & wildcards.
eg. To check if the any process is running with following criteria:
tasklist.exe /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2" | find /v "No task" >nul && (echo process exists) || (echo na man).
Above method can also find if any document (window) is open, in addition to the underlying process, like "firefox.exe".
eg. close high speed vpn ad window if/when it pops up without notice:
tasklist /fi "windowtitle eq High-Speed*" | find /v "No task" >nul && (taskkill /fi "windowtitle eq High-Speed*")
Tested on Win 10 CMD