My code needs to check if notepad.exe
is listed under tasklist
. If it is, the batch file will close it.
@echo off
tasklist /fi "imagename eq notepad.exe" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit
tasklist
always returns an errorlevel of 0, even when it doesn't find anything. In order to work around this, you should instead pipe the output of tasklist
to find
, which will return an errorlevel of 0 if the process is found and 1 if the process is not found.
@echo off
tasklist | find /I "notepad.exe" >nul
if "%errorlevel%"=="0" taskkill /f /im "notepad.exe"