Solved

Batch File to check if program is running

Posted on 2007-11-20
5
12,581 Views
Last Modified: 2009-10-03
I want to write a batch file to check if a program is running by the Process Name in the Task Manager. If not running I would like to start that program.
0
Comment
Question by:sonu19
  • 3
5 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20322736
@echo off

setlocal

set exeName=%~1

if "%~1"=="" set /p exeName=Enter program name to check:

if "%exeName%"=="" goto :EOF

set found=

for /f "tokens=*" %%a in ('tasklist ^| findstr /i "%exeName%"') do set found=Y

if "%found%"=="Y" goto :EOF

echo Running %exeName%
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20322767
Here's an updated version that will allow you to fully qualify the name. Also, here's the version that actual runs the process:

@echo off

setlocal

set exeName=%~1

if "%~1"=="" set /p exeName=Enter program name to check:

if "%exeName%"=="" goto :EOF

call :GETNAME "%exeName%"

set found=

for /f "tokens=*" %%a in ('tasklist ^| findstr /i "%fileName%"') do set found=Y

if "%found%"=="Y" goto :EOF

echo Running %exeName%

start "%exeName%"

goto :EOF

:GETNAME

set fileName=%~nx1

goto :EOF
0
 

Author Comment

by:sonu19
ID: 20323610
when i enter the program name it just close the window
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
ID: 20323952
That's what it will do if the process is already running. You have to enter a process that isn't running. Here's a version that will print out debugging information and will pause. You must be double clicking on the batch processing. I like to test in an open cmd window.

@echo off

setlocal

set exeName=%~1

if "%~1"=="" set /p exeName=Enter program name to check:

if "%exeName%"=="" echo Processing aborted&pause&goto :EOF

call :GETNAME "%exeName%"

set found=

for /f "tokens=*" %%a in ('tasklist ^| findstr /i "%fileName%"') do set found=Y

echo exeName=%exeName%
echo fileName=%fileName%
echo found=%found%

if "%found%"=="Y" echo Process is running&pause&goto :EOF

echo Running %exeName%

start "%exeName%"

pause

goto :EOF

:GETNAME

set fileName=%~nx1

goto :EOF
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20327607
SteveGTR's solution will work like a charm.  FWIW, you could also do it non-interactively like this -

tasklist | find /i "notepad.exe" || notepad.exe

... replace 'notepad.exe' with the process you're checking for.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question