Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Batch File to check if program is running

Posted on 2007-11-20
5
Medium Priority
?
12,609 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

704 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