Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 ?

share|improve this question
1  
You can pipe tasklist into findstr and run off the error code, sorry on phone so can't post example –  50-3 Nov 5 '13 at 17:57
1  
tasklist | findstr program.exe you will get back one line if found, and no lines if not. –  Frank Thomas Nov 5 '13 at 18:03
    
Your example pseudo code is checking if the FILE program.exe exists on disk, not if it has a process in memory. PS: it'd be `If NOT EXIST program.exe'. –  Ƭᴇcʜιᴇ007 Nov 5 '13 at 18:08

2 Answers 2

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.

share|improve this answer
    
The single line is "if "%ERRORLEVEL%"=="0" echo Program is running.". It will only run the Echo command if the result is True (errorlevel is 0). There is no "next line of code". While it would run regardless if it did exist, it's only because it's outside of the IF block. :) –  Ƭᴇcʜιᴇ007 Nov 5 '13 at 21:26
    
Also, in Windows Vista (7?) and above, the :eof label is inbuilt into Goto now: "GOTO command now accepts a target label of :EOF which transfers control to the end of the current batch script file. This is an easy way to exit a batch script file without defining a label." (goto /?) :) –  Ƭᴇcʜιᴇ007 Nov 5 '13 at 21:27
    
GOTO:eof is the one I think you mean and yes it does work on most systems I just prefer to use the extended method for the sake of teaching –  50-3 Nov 5 '13 at 22:00

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.