?
Solved

Windows 7 command script help (n00bie)

Posted on 2011-10-17
12
Medium Priority
?
601 Views
Last Modified: 2012-08-13
Hi All,

I'm trying to make a script that will run an .exe if its not running already.  Here are the two commands that start and stop the program.

Start program:
"C:\Program Files (x86)\XBMC\XBMC.exe"

Stop program:
taskkill /IM XBMC.exe


So I need to check if  XBMC.exe is running, and if it isn't run the start command, if it is run the stop command.


many thanks.
0
Comment
Question by:detox1978
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 36978956
Try this batch script (Whatever.cmd); it currently just starts/stops the calculator, so that you can test it.
@echo off
setlocal
REM set Command=C:\Program Files (x86)\XBMC\XBMC.exe
set Command=C:\Windows\system32\calc.exe
:: ========================================
for %%a in ("%Command%") do (set ImageName=%%~nxa)
echo ImageName: %ImageName%
tasklist /FI "IMAGENAME eq %ImageName%" | find /i "%ImageName%"
if errorlevel 1 (
  echo Starting %Command% ...
  start "" "%Command%"
) else (
  echo Stopping %Command% ...
  taskkill /IM "%ImageName%"
)

Open in new window

0
 
LVL 12

Accepted Solution

by:
GusGallows earned 668 total points
ID: 36978958
This would be the easiest way:
$Path = "C:\Program Files (x86)\XBMC\XBMC.exe"
$result = Get-Process | where {$_.Path -eq $Path}
if ($result -eq $null)
{
	start-Process $Path	
}
Else
{
	$name = $result.Name
	Stop-Process -Name $name -force
}

Open in new window

0
 
LVL 12

Expert Comment

by:GusGallows
ID: 36978964
Sorry posted at the same time. Mine is the Powershell method. oBdA's is using windows shell. Either should suit your purpose.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

Expert Comment

by:paultomasi
ID: 36979023
GusGallows

Never looked at Powershell before... does it also work like this (see the IF and braces, and case)?

$path = "C:\Program Files (x86)\XBMC\XBMC.exe"
$result = Get-Process | where {$_.Path -eq $Path}

if ($result -eq $null) {
   start-process $path
} else {
   $name = $result.name
   stop-process -name $name -force
}

Open in new window

0
 
LVL 2

Author Comment

by:detox1978
ID: 36979049
Neither worked for me.

RThe example with the calc did,but when i removed it for the .exe it didn't.


Not sure why the powershell didn't work, it returned this error message;

The term 'xbmc.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the sp
elling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:9
+ xbmc.ps1 <<<<
    + CategoryInfo          : ObjectNotFound: (xbmc.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


Suggestion [3,General]: The command xbmc.ps1 was not found, but does exist in the current location. Windows PowerShell d
oesn't load commands from the current location by default. If you trust this command, instead type ".\xbmc.ps1". See "ge
t-help about_Command_Precedence" for more details.

Open in new window

0
 
LVL 2

Author Comment

by:detox1978
ID: 36979070
just realised its a clean PC build so had powersell restriction enabled, switched it off using "Set-ExecutionPolicy RemoteSigned" and it run fine.
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 668 total points
ID: 36979073
You can do it as one command too to detect if the task is running and if not start it for instance:

tasklist | find /i "xmbc.exe" >NUL  || START "" xmbc.exe

The || means if the previous error returned an errorlevel then run.  In this case with find 0 means it HAS found the request, so if it doesn't find the string in tasklist output then it isn't running so it needs to start it...

@echo off
cd /d c:\dirwith\xmbcin
set process=xmbc.exe
tasklist | find /i "%process%" >NUL  || START "" %process% & exit /b
taskill /im %process%

Steve
0
 
LVL 85

Assisted Solution

by:oBdA
oBdA earned 664 total points
ID: 36979098
Sorry, an issue with "echo" and round brackets; try this:
@echo off
setlocal
set Command=C:\Program Files (x86)\XBMC\XBMC.exe
REM set Command=C:\Windows\system32\calc.exe
:: ========================================
for %%a in ("%Command%") do (set ImageName=%%~nxa)
tasklist /FI "IMAGENAME eq %ImageName%" | find /i "%ImageName%"
if errorlevel 1 (
  echo Starting %ImageName% ...
  start "" "%Command%"
) else (
  echo Stopping %ImageName% ...
  taskkill /IM "%ImageName%"
)

Open in new window

0
 
LVL 2

Author Comment

by:detox1978
ID: 36979113
thanks very much
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36979121
Thanks for the assisted there, wasn't trying to jump in on good solution you already had!

Steve
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36984301
detox1978

I don't deserve points here as I was merely repeating someone else's code. I am quite happy for you to re-open this question and re-assign the points.

I have asked a moderator to assist you.

Thank you.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Loops Section Overview

850 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