33

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?

Alex.K.
  • 3,342
  • 13
  • 38
  • 44
user2176930
  • 333
  • 1
  • 3
  • 4

5 Answers5

57

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

Magoo
  • 68,705
  • 7
  • 55
  • 76
  • 5
    Rather than checking `if errorlevel`, this can be even further simplified with [conditional execution](http://www.robvanderwoude.com/condexec.php). – rojo Mar 16 '13 at 13:17
  • 2
    Perhaps it's better to `find "notepad.exe"` since the output is translated and we cannot be sure whether `:` appears in all languages. – Thomas Weller Sep 02 '15 at 06:23
23

This is a one line solution.

It will run taskkill only if the process is really running otherwise it will just info that it is not running.

tasklist | find /i "notepad.exe" && taskkill /im notepad.exe /F || echo process "notepad.exe" not running.

This is the output in case the process was running:

notepad.exe           1960 Console                   0    112,260 K
SUCCESS: The process "notepad.exe" with PID 1960 has been terminated.

This is the output in case not running:

process "notepad.exe" not running.
nicovota
  • 330
  • 2
  • 4
9

TASKLIST doesn't set an exit code that you could check in a batch file. One workaround to checking the exit code could be parsing its standard output (which you are presently redirecting to NUL). Apparently, if the process is found, TASKLIST will display its details, which include the image name too. Therefore, you could just use FIND or FINDSTR to check if the TASKLIST's output contains the name you have specified in the request. Both FIND and FINDSTR set a non-null exit code if the search was unsuccessful. So, this would work:

@echo off
tasklist /fi "imagename eq notepad.exe" | find /i "notepad.exe" > nul
if not errorlevel 1 (taskkill /f /im "notepad.exe") else (
  specific commands to perform if the process was not found
)
exit

There's also an alternative that doesn't involve TASKLIST at all. Unlike TASKLIST, TASKKILL does set an exit code. In particular, if it couldn't terminate a process because it simply didn't exist, it would set the exit code of 128. You could check for that code to perform your specific actions that you might need to perform in case the specified process didn't exist:

@echo off
taskkill /f /im "notepad.exe" > nul
if errorlevel 128 (
  specific commands to perform if the process
  was not terminated because it was not found
)
exit
Andriy M
  • 71,352
  • 17
  • 87
  • 142
  • I found if the image name, includes the file extension, over 25 characters, the "Image Name" column in the tasklist output will be truncated, which makes the "find" command with image name doesn't work. However, detect only by the ":" character seems too loose, I am afraid it may happens in the positive output of the future versions. Is there any other better way to do so? – Elliott Jan 23 '17 at 10:18
  • 1
    @Elliott: If you store the executable's name in a variable, you can then use the whole value in `tasklist` and only the first 25 chars in `find`: `tasklist /fi "imagename eq %imgname%" | find /i "%imgname:~0,25%" > nul` – Andriy M Jan 24 '17 at 06:45
2

That's why it's not working because you code something that is not right, that's why it always exit and the script executer will read it as not operable batch file that prevent it to exit and stop so it must be

tasklist /fi "IMAGENAME eq Notepad.exe" 2>NUL | find /I /N "Notepad.exe">NUL
if "%ERRORLEVEL%"=="0" (
msg * Program is running
goto Exit
)
else if "%ERRORLEVEL%"=="1" (
msg * Program is not running
goto Exit
)

rather than

@echo off
tasklist /fi "imagename eq notepad.exe" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit
Single Entity
  • 2,289
  • 3
  • 30
  • 59
-3

Try this:

@echo off
set run=
tasklist /fi "imagename eq notepad.exe" | find ":" > nul
if errorlevel 1 set run=yes
if "%run%"=="yes" echo notepad is running
if "%run%"=="" echo notepad is not running
pause
Didier Aupest
  • 2,840
  • 2
  • 22
  • 32
Cyber
  • 1
  • 1