Here is my suggestion:
@echo off
tasklist /FI "IMAGENAME eq program.exe" | findstr "program.exe" >nul
if %ERRORLEVEL% == 1 goto mycode
goto eof
:mycode
rem put your code here
:eof
exit
Like @techie007 suggested I would recommend tasklist /FI "IMAGENAME eq program.exe"
as it only outputs exact matches so when you're searching cmd.exe
you're not hitting htcmd.exe
not sure why he NULLs it though as it's being piped.
I'm not sure why he is suggesting find /I /N
I personally prefer findstr
as it's much more versatile but in his suggestion it gets the job done.
The last part is where my suggestion would differ from @techie007's if %ERRORLEVEL% == 1 goto mycode
goto eof
this will work as a if else statement so if the program is not running go to mycode
(Where you will fill with well... your code) else go to eof
which will exit the batch.
My only fault with @techie007's is if "%ERRORLEVEL%"=="0" echo Program is running.
as regardless of if this is true or false it will run the next line of code. This is why I wanted to recommend using a goto
methodology as it will support multiple lines of code.
tasklist | findstr program.exe
you will get back one line if found, and no lines if not.