本文介绍了杀死一个过程,批次和成功报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的批处理文件,它终止iTunes程序的话,如果我连我的iPod,它不会同步的。 (我知道你可以在iTunes设置起来。)

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

但是,> NUL 不响应该命令;所以它只是给出了默认的命令文本。所以,是的,我想要做的:

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

如果没有找到的iTunes,由命令给出,它应该显示

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

iTunes中没有找到

如果它被发现,终止,

iTunes中被杀害

帮助?的不工作的错误级别,这似乎是错的 NUL 不工作。

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

推荐答案

作品至少对我来说:

> taskkill /f /im powershell.exe && echo worked || echo not worked
SUCCESS: The process "powershell.exe" with PID 3228 has been terminated.
worked

> taskkill /f /im powershell.exe && echo worked || echo not worked
ERROR: The process "powershell.exe" not found.
not worked

所以 TASKKILL 的返回正确的退出code。其输出重定向无关与此有关。但失败的错误级别为128。你真的应该使用正确的成语检查错误。

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.

此外,它似乎 TASKKILL 被打印到标准错误让你看到它的输出是,当在重定向标准输出。您可以考虑上述code重写:

Also it seems that taskkill is printing to stderr so you see its output still, when just redirecting stdout. You may consider rewriting above code to:

taskkill /F /IM itunes.exe >nul 2>&1
if errorlevel 1 (echo iTunes not found.) else (echo iTunes is killed.)

2 - ;&放大器; 1 重定向标准错误输出到广阔的虚无。 如果ERRORLEVEL 1 错误级别检查是的至少1 的应该在这一点上的工作:

The 2>&1 redirects the standard error output into the vast nothingness. if errorlevel 1 checks for errorlevel being at least 1 which should work at this point:

ERRORLEVEL号如果最后运行的程序返回退出code等于或大于指定的号码大的真实情况。 - 帮助,如果

一般检查错误级别如果%ERRORLEVEL%= = 是一个相当糟糕的主意,除非你比较0出境codeS的语义是什么非零信号故障。假设你刚刚在这里是 TASKKILL 将返回 1 失败。

Generally checking errorlevel with if %errorlevel%== is a quite bad idea, unless you're comparing to 0. The semantics for exit codes are that anything non-zero signals failure. Your assumption here just was that taskkill would return 1 on failure.

答可能我恳请你为什么无限循环这样做呢? TASKKILL 已经杀死的所有 itunes.exe 的实例。而你在紧密循环,没有任何延迟运行,以便您的批处理文件可能占用一个CPU核心,而它的运行。

Ans may I kindly ask why you are doing this in an endless loop? taskkill already kills all instances of itunes.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:忽视了你的编辑:为什么地球上大括号?在批处理文件中块由圆括号delimieted。

ETA: Overlooked your edit: Why on earth curly braces? Blocks in batch files are delimieted by round parentheses.

这篇关于杀死一个过程,批次和成功报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:18