Solved

Batch File to check if program is running

Posted on 2007-11-20
5
12,563 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
Comment Utility
@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
Comment Utility
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
Comment Utility
when i enter the program name it just close the window
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

16 Experts available now in Live!

Get 1:1 Help Now