views:

467

answers:

2

Hello, i have the following batch file, which terminates the iTunes program so, that if i connect my iPod, it's not going to sync it. (I know you can set this up in iTunes.)

@echo off
:kill
cls
taskkill /F /IM itunes.exe >nul
if %errorlevel%==1 {
echo iTunes not found.
} else {
echo iTunes is killed.
}
goto kill

However, the >nul does not respond to the command; so it just gives the default command text. So yeah, what i want to do:

If iTunes is not found, as given by the command, it should display

iTunes not found

If it is found and terminated,

iTunes is killed

Help? the errorlevel's don't work, this seem to be the fault of the nul not working.

+4  A: 
Joey
`why on eart curly braces`, well, i use them as long as i worked with them. I don't know; i just like it. It looks nicer i guess.
YourComputerHelpZ
Well, if you're using them in batch files, then expect things going wrong. If you write code in a language you have to use the language's grammar, regardless what looks nicer or not. Curly braces don't work as block delimiters in batch.
Joey
i thought they did. At least they worked for the codes i used. I work with batch for over 1 year now, and have developed with curly braces, over and over, and it works fine. My customers (aka youtubers ;)) say it works fine for them.
YourComputerHelpZ
altough, thanks for sharing this. it works fine.
YourComputerHelpZ
oh, and about the endless loop: i edited the thing so that at `else` it says: `else(goto :killed)`. In `:killed`, we have an additional 2 times taskkill, just to be sure, and after that, it echo a text, and tells the user it should press any key to `exit`.
YourComputerHelpZ
A: 

an alternative solution, in vbscript

Set objArgs = WScript.Arguments
strProcess = objArgs(0)
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name ='" & strProcess & "'")

If colProcesses.Count = 0 Then
    Wscript.Echo strProcess & " is not running."
Else
    Wscript.Echo strProcess & " is running."
    'Kill the process
    For Each objProcess in colProcesses
        r=objProcess.Terminate()
        WScript.Echo "r is " & r
        If r = 0 Then
         WScript.Echo strProcess & " killed"
        End 
    Next     
End If

on the command line

c:\test> cscript //nologo mykill.vbs "itunes.exe"
ghostdog74