Windows Scripting

Is there a way to write a windows scripting script to detect if a specific application is open & if so to close it?

Can the converse be done, that is, if application is NOT running, start it?

Thanks
Richard KortsBusiness Owner / Chief DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITEnd-user supportCommented:
To check for chrome.exe:
tasklist /FI "IMAGENAME eq chrome.exe"

Open in new window

Steve KnightIT ConsultancyCommented:
If this is a service on a PC then it needs handling differently using SC or net stop / net stop commands but if it is just an executable then you can forcibly close it using:

taskkill /f /IM progname.exe

Open in new window


and start it again with

START "" "c:\dir\progname.exe"

Open in new window


and see if it is there or not with:

tasklist /FI "ImageName eq progname.exe"

Open in new window


So this for instance will check if notepad.exe is started then tell you and stop it forcibly.  Then restart it.

Steve

@echo off
set prog=notepad.exe
set startprog="c:\windows\%prog%"

tasklist /FI "ImageName eq %prog%" | find /i "%prog%" && (
  echo %prog% is running currently, stop it:
  taskkill /f /IM "%prog%"
)
echo Starting program %prog%
START "" %startprog%

pause

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
Steve Knight,

Great.

Now, to show you my naivete, do I create a txt file with that sor of commands in it & make it a Windows Scheduled Task run when I want to?

Thanks
NVITEnd-user supportCommented:
FYI: You can give full points to Steve. I'm just helping out here...

One way is...

Change Steve's code a bit:
Note: I removed the PAUSE because that won't work.
@echo off
set prog=%1
set startprog=%prog%"

tasklist /FI "ImageName eq %prog%" | find /i "%prog%" && (
  echo %prog% is running currently, stop it:
  taskkill /f /IM "%prog%"
)
echo Starting program %prog%
START "" %startprog%

Open in new window


Make your progs.txt file with the program names. e.g.
notepad.exe
chrome.exe

Open in new window


Run it from Task Scheduler:
Program: cmd
Arguments: /c for /f %a in (c:\dir\progs.txt) do (call c:\dir\Steves.bat %a)
Steve KnightIT ConsultancyCommented:
Thanks and spilt points fine! I'm out at mo so on mobile if anything extra needed just ask and one of us can clarify.

Steve
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.