Solved

Batch File to check if program is running

Posted on 2007-11-20
5
12,570 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now