Link to home
Start Free TrialLog in
Avatar of detox1978
detox1978Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Windows 7 command script help (n00bie)

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.
Avatar of oBdA
oBdA

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

ASKER CERTIFIED SOLUTION
Avatar of GusGallows
GusGallows
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry posted at the same time. Mine is the Powershell method. oBdA's is using windows shell. Either should suit your purpose.
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

Avatar of detox1978

ASKER

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

just realised its a clean PC build so had powersell restriction enabled, switched it off using "Set-ExecutionPolicy RemoteSigned" and it run fine.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks very much
Thanks for the assisted there, wasn't trying to jump in on good solution you already had!

Steve
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.