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.
an alternative solution, in vbscript, save below code as mykill.vbs
on the command line
Why don't you use PowerShell?
Works for me at least:
So
taskkill
is returning a proper exit code. The redirect of its output has nothing to do with this. But the error level for failure is 128. You really should use the proper idiom for checking for errors.Also it seems that
taskkill
is printing tostderr
so you see its output still, when just redirectingstdout
. You may consider rewriting above code to:The
2>&1
redirects the standard error output into the vast nothingness.if errorlevel 1
checks forerrorlevel
being at least 1 which should work at this point:Generally checking
errorlevel
withif %errorlevel%==
is a quite bad idea, unless you're comparing to 0. The semantics for exit codes are thatanything
non-zero signals failure. Your assumption here just was thattaskkill
would return1
on failure.Ans may I kindly ask why you are doing this in an endless loop?
taskkill
already kills all instances ofitunes.exe
. And you're running in a tight loop without any delays so your batch files probably consumes one CPU core while it's running.ETA: Overlooked your edit: Why on earth curly braces? Blocks in batch files are delimieted by round parentheses.