Solved

Batch File to check if program is running

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

Suggested Solutions

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

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