Windows – Batch IF .exe is not running

batchbatch filecommand linewindows

I am trying to create a batch that IF program.exe is not running in process, it will run code below. In vb.net this is easy, but I do not want to create a program. I am guessing a VBS script would work, but I am trying to see if I can create a batch routine to do the above.

.bat:

@ECHO OFF

REM peudo code:

If NOT program.exe Exist

'Do something.

The above does not work, so what am I missing ?

Best Answer

Here ya go:

@echo off
tasklist /FI "IMAGENAME eq program.exe" 2>NUL | find /I /N "program.exe" >NUL
if "%ERRORLEVEL%"=="0" echo Program is running.

It uses Tasklist.exe to get a list of tasks that match your program name, then uses find to figure out if the program actually exists in the list.

It then uses the error code returned by find to determine if it was found, and if so executes the code (the echo in this case).

Output from Tasklist and Find are sent to NULL.

Source

Related Question