将 ERRORLEVEL 重置为零的最简单方法是什么?
我有一个构建后事件,为 ac# 项目运行一些命令。 最后一个命令有时会导致 ERRORLEVEL 值不等于 0,然后构建失败。
我想附加一行额外的命令以始终将 ERRORLEVEL 值设置为零。 最方便的方法是什么?
I have a post-build event that runs some commands for a c# project. The last command would sometimes cause the ERRORLEVEL value not equals to zero and then the build fails.
I want to append an extra line of command to always set the ERRORLEVEL value to zero. What is the most convenient way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
如果您使用
exit /b 0
,您可以从子批处理脚本中返回errorlevel 0
,而无需退出父批处理脚本。if you use
exit /b 0
you can return anerrorlevel 0
from within a child batch script without also exiting the parent.似乎成功了:
并非一切都有效,而且还不清楚原因。 例如,以下情况则不然:
Seems to do the trick:
Not everything works, and it is not clear why. For example, the following do not:
在构建前或构建后事件中,如果可执行文件的返回代码大于零,并且对可执行文件的调用不是构建前或构建后事件的最后一行,则可以快速将其静音并避免触发非零
errorlevel
检查的方法是在失败的行后面加上显式返回零的行:这本质上是前面提到的解决方案的通用组合,适用于超过 < em>只是构建前或构建后事件的最后一行。
In a pre- or post-build event, if the return code of an executable is greater than zero, and the call to the executable is not the last line of the pre- or post-build event, a quick way to mute it and avoid triggering a check for a non-zero
errorlevel
is to follow the failing line with a line that explicitly returns zero:This is essentially a generic combination of the previously-mentioned solutions that will work with more than just the last line of a pre- or post-build event.
我个人使用这个:
cd .
即使在 unix shell 中也可以工作。
但是,这个可能会快一点:
type nul>nul
因为
Process Monitor
显示cd 上的
QueryDirectory
调用。附注:
cd .
在 unix shell 中还有另一个很好的副作用。 如果在擦除之前已打开工作目录,它会恢复终端中重新创建的工作目录。更新:
这更快一点:
有任何 Windows 命令可以查找文件是否正在使用吗?
:https://www.dostips。 com/forum/viewtopic.php?t=5542
I personally use this:
cd .
Works even in unix shell.
But, this one might be a bit faster:
type nul>nul
Because
Process Monitor
showsQueryDirectory
calls oncd .
PS:
cd .
has another nice side effect in the unix shell. It does restore recreated working directory in the terminal if it has been opened before the erase.Update:
And that is a bit more faster:
Any windows command to find if a file is in use or not ?
:https://www.dostips.com/forum/viewtopic.php?t=5542
我发现“exit 0”看起来是解决这个问题的好方法。
使用示例:
如果未启动 UnderDevService 服务,则
I found that "exit 0" looks like a good way to deal with this problem.
Usage Example:
if the UnderDevService service is not started.
我使用
VERIFY
或VERIFY > 空
I use
VERIFY
orVERIFY > nul
如果这是像“构建后事件”等这样的片段,那么您可以
在最后一个命令的末尾附加:。
另一种方式
非常干净且非惯用——了解 Windows shell 的读者会知道发生了什么以及您的意图是什么。
但是,如果您使用批处理脚本,则可能需要使用子程序,它是 akf 答案中的“子批处理脚本”的轻量级等效项。
有一个子例程:
然后
在您需要的地方使用它。
这是一个完整的示例:
哪个输出:
如您所见 - 仅通过 goto:eof 调用并返回是不够的。
If this is a snippet like "Post-build Event" etc., then you'll be fine appending:
at the end of the last command.
Alternatively
is very clean and non-idiomatic -- a reader who knows Windows shell will know what's going on, and what was your intent.
However, if you're in a batch script, you may want to use subrotines, which are a lightweight equivalent of the "child batch script" from akf's answer.
Have a subroutine:
and then just
wherever you need it.
Here's a complete example:
Which outputs:
As you see - just calling and returning via goto:eof is not enough.
以下内容适用于具有
cmd.exe
的现代 Windows(基于 NT)系统:SPACE(或更准确地说,一个或多个标准标记分隔符的任意序列,分别是 SPACE(代码
0x20
)、TAB(代码0x09
)、、
,;
,=
, NBSP (代码0xFF
),VTAB (代码 <代码>0x0B)和FF(代码0x0C
))是强制性的; 如果省略它,则设置ErrorLevel
:DosTips.com 出现了这种技术。
这是另一种方法,但它访问文件系统,因此可能会慢一些:
The following works in modern Windows (NT-based) systems that feature
cmd.exe
:The SPACE (or more precisely, an arbitrary sequence of one or more standard token separators, which are SPACE (code
0x20
), TAB (code0x09
),,
,;
,=
, NBSP (code0xFF
), VTAB (code0x0B
) and FF (code0x0C
)) is mandatory; if you omit it theErrorLevel
becomes set instead:There is a nice thread on DosTips.com where this technique came up.
Here is an alternative method, but which accesses the file system and might therefore be a bit slower:
以下是重置
ErrorLevel
状态的一些其他方法,这些方法甚至适用于 MS-DOS(至少对于版本 6.22):以下方法仅适用于 MS-DOS:
为了完整起见,这将
ErrorLevel
状态设置为1
,对 Windows 和 MS-DOS 均有效:Here are some other ways to reset the
ErrorLevel
state, which even work in MS-DOS (at least for version 6.22):The following methods work in MS-DOS only:
For the sake of completeness, this sets the
ErrorLevel
state to1
, valid for both Windows and MS-DOS:在查看了所有其他答案后,我决定找出哪种方法重置错误级别最有效。 我制作了一个快速脚本,记录了执行每个脚本的时间:
这是输出:
,我发现它只真正接受
cd .
和cmd /c "exit /b 0"
--我做错了什么吗?我推荐
cd .
或type nul
因为它们在控制台的输出上都没有足迹,而且在任何方面都不慢。是的,我很无聊
After reviewing all of the other answers, I decided to find which way was the most efficient for resetting the ERRORLEVEL. I made a quick script that recorded the time to execute each of these:
here is the output:
after reviewing with
Measure-Command {command}
in Powershell, I found that it only really acceptedcd .
andcmd /c "exit /b 0"
--am I doing something wrong?I'd recommend either
cd .
ortype nul
since neither have a footprint on the output of the console, nor are they slow in any measure.yeah I'm pretty bored
在每个可能失败的命令后面添加
>nul
- 这似乎可以防止构建失败。您仍然可以通过检查
%errorlevel%
来检查命令的结果。例如:
Add
>nul
after each command that's likely to fail - this seems to prevent the build from failing.You can still check the result of the command by examining
%errorlevel%
.For example:
用户 aschipfl 给出了最佳答案,使用最小的函数 FIND 来完成它:
此行将在 DOS 或更高版本中将 ERRORLEVEL 设置为 1,在 DOS 或更高版本中:
此行将在 DOS 或更高版本中将 ERRORLEVEL 设置为 0:
User aschipfl gave best answer, to complete it, using smallest function FIND:
This line will set ERRORLEVEL to 1, in DOS or later:
This line will set ERRORLEVEL to 0, in DOS or later:
(重新)设置 %ERRORLEVEL% 的一个有趣选项是使用本地函数(这与 @akf 的建议类似):
尽管将
ERRORLEVEL
重置为 0(表示成功)是最有效的常见用法,您可以使用它来设置任何特定错误。One interesting option to (re)set %ERRORLEVEL% is to use a local function (this would be similar to the suggestion from @akf):
Although resetting
ERRORLEVEL
to 0 (indicating success) would be the most common usage, you could use this to set any specific error.尝试了上述所有解决方案 - 没有任何效果。 但在批处理文件中找到了适合我的解决方案:
(/A 开关将提供的值定义为数字表达式)
Tried all solutions above - nothing worked. But found this solution that works for me inside the batch file:
(the /A switch defines the supplied value as a numerical expression)
我正在使用这个:
ping localhost -n 1 > null
I'm using this:
ping localhost -n 1 >null
我总是只是用;
我已经使用它很多年了。
I always just used;
I've been using it for donkey's years.