2

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 ?

3
  • 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, 2013 at 17:57
  • 1
    tasklist | findstr program.exe you will get back one line if found, and no lines if not. Nov 5, 2013 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'. Nov 5, 2013 at 18:08

4 Answers 4

5

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

1

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.

3
  • 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. :) Nov 5, 2013 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 /?) :) Nov 5, 2013 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, 2013 at 22:00
0
echo off
SETLOCAL EnableExtensions
set EXE=YourProgram.exe
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto FOUND
echo Not running
pause
goto FIN
:FOUND
echo Running
pause
:FIN

This worked for me.

0

The following work for me in windows 7.

tasklist | findstr /i "appName.exe" > nul && echo Yeah || echo Nope

The command(s) after && on the same line will run when findstr found the "appName.exe" and skip the command(s) after the || on the same line. Otherwise, when findstr failed to find the "appName.exe", it will skip commands after && and execute those after ||.

If you want multiple commands for success and/or failure:

tasklist | findstr /i "appName.exe" > nul && ( 
    echo.Yeah
    echo.It's running.
) || (
    echo.Nope
    echo.Don't hope.
)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .