Batch program to to check if process exists
batch-file, cmd, command, windows
Solution
`TASKLIST` does not set errorlevel.
echo off
tasklist /fi "imagename eq notepad.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit
should do the job, since ":" should appear in `TASKLIST` output only if the task is NOT found, hence `FIND` will set the errorlevel to `0` for `not found` and `1` for `found`
Nevertheless,
taskkill /f /im "notepad.exe"
will kill a notepad task if it exists - it can do nothing if no notepad task exists, so you don't really need to test - unless there's something else you want to do...like perhaps
echo off
tasklist /fi "imagename eq notepad.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"&exit
which would appear to do as you ask - kill the notepad process if it exists, then exit - otherwise continue with the batch
Problem
I want a batch program, which will check if the process `notepad.exe` exists. if `notepad.exe` exists, it will end the process, else the batch program will close itself. Here is what I've done: ``` @echo off tasklist /fi "imagename eq notepad.exe" > nul if errorlevel 1 taskkill /f /im "notepad.exe" exit ``` But it doesn't work. What is the wrong in my code?