windows 将 ERRORLEVEL 重置为零的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1113727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the easiest way to reset ERRORLEVEL to zero?
提问by user95319
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.
我有一个构建后事件,它为 ac# 项目运行一些命令。最后一个命令有时会导致 ERRORLEVEL 值不等于零,然后构建失败。
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?
我想附加一行额外的命令以始终将 ERRORLEVEL 值设置为零。最方便的方法是什么?
采纳答案by user95319
I found that "exit 0" looks like a good way to deal with this problem.
我发现“exit 0”看起来是处理这个问题的好方法。
Usage Example:
用法示例:
NET STOP UnderDevService /Y
exit 0
NET 停止 UnderDevService /Y
退出 0
if the UnderDevService service is not started.
如果未启动 UnderDevService 服务。
回答by akf
if you use exit /b 0
you can return an errorlevel 0
from within a child batch script without also exiting the parent.
如果您使用,exit /b 0
您可以errorlevel 0
从子批处理脚本中返回一个,而无需退出父脚本。
回答by Jason Kresowaty
Seems to do the trick:
似乎可以解决问题:
ver > nul
Not everything works, and it is not clear why. For example, the following do not:
并非一切都有效,目前尚不清楚原因。例如,以下情况不会:
echo. > nul
cls > nul
回答by Jeffrey Wilges
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 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:
在构建前或构建后事件中,如果可执行文件的返回代码大于零,并且对可执行文件的调用不是构建前或构建后事件的最后一行,则快速将其静音并避免触发对非零的检查errorlevel
是在失败的行后面跟随显式返回零的行:
cmd /c "exit /b 0"
This is essentially a generic combination of the previously-mentioned solutions that will work with more than justthe last line of a pre- or post-build event.
这本质上是前面提到的解决方案的通用组合,不仅适用于构建前或构建后事件的最后一行。
回答by Andry
I personally use this:
我个人使用这个:
cd .
cd .
Works even in unix shell.
即使在 unix shell 中也能工作。
But, this one might be a bit faster:
但是,这个可能会快一点:
type nul>nul
type nul>nul
Because Process Monitor
shows QueryDirectory
calls on cd .
因为Process Monitor
显示QueryDirectory
调用cd .
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.
PS:cd .
在 unix shell 中有另一个不错的副作用。如果在擦除之前已打开,它会在终端中恢复重新创建的工作目录。
回答by ixe013
I use VERIFY
or VERIFY > nul
我使用VERIFY
或VERIFY > nul
回答by Tomasz Gandor
If this is a snippet like "Post-build Event" etc., then you'll be fine appending:
如果这是“构建后事件”等片段,那么您可以附加:
(...) || ver > nul
at the end of the last command.
在最后一个命令的末尾。
Alternatively
或者
cmd /c "exit /b 0"
is very clean and non-idiomatic -- a reader who knows Windows shell will know what's going on, and what was your intent.
非常干净且不惯用语——了解 Windows shell 的读者会知道发生了什么,以及您的意图是什么。
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.
但是,如果您使用的是批处理脚本,则可能需要使用子程序,它是 akf 答案中“子批处理脚本”的轻量级等效项。
Have a subroutine:
有一个子程序:
:reset_error
exit /b 0
and then just
然后只是
call :reset_error
wherever you need it.
无论你需要它。
Here's a complete example:
这是一个完整的例子:
@echo off
rem *** main ***
call :raise_error
echo After :raise_error ERRORLEVEL = %ERRORLEVEL%
call :empty
echo After :empty ERRORLEVEL = %ERRORLEVEL%
call :reset_error
echo After :reset_error ERRORLEVEL = %ERRORLEVEL%
:: this is needed at the end of the main body of the script
goto:eof
rem *** subroutines ***
:empty
goto:eof
:raise_error
exit /b 1
:reset_error
exit /b 0
Which outputs:
哪些输出:
After :raise_error ERRORLEVEL = 1
After :empty ERRORLEVEL = 1
After :reset_error ERRORLEVEL = 0
As you see - just calling and returning via goto:eof is not enough.
如您所见 - 仅通过 goto:eof 调用和返回是不够的。
回答by aschipfl
Here are some other ways to reset the ErrorLevel
state, which even work in MS-DOS (at least for version 6.22):
以下是一些其他重置ErrorLevel
状态的方法,它们甚至可以在 MS-DOS 中使用(至少对于版本 6.22):
more < nul > nul
rem // The `> nul` part can be omitted in Windows but is needed in MS-DOS to avoid a line-break to be returned:
sort < nul > nul
The following methods work in MS-DOS only:
以下方法仅适用于 MS-DOS:
command /? > nul
fc nul nul > nul
For the sake of completeness, this sets the ErrorLevel
state to 1
, valid for both Windows and MS-DOS:
为了完整起见,这将ErrorLevel
状态设置为1
,对 Windows 和 MS-DOS 都有效:
< nul find ""
回答by alexlyee
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:
在查看了所有其他答案之后,我决定找出最有效的重置 ERRORLEVEL 的方法。我制作了一个快速脚本,记录了执行每个脚本的时间:
"cmd /c "exit /b 0"", "cd .", "ver", "type nul", and "VERIFY"
here is the output:
这是输出:
cmd /v:on /c set ^"q=^"^" & timeit.cmd "cmd /c ^!q^!exit /b 0^!q^!" "cd ." "ver" "type nul" "VERIFY"
cmd /c "exit /b 0" took 0:0:0.02 (0.02s total)
cd . took 0:0:0.00 (0.00s total)
Microsoft Windows [Version 10.0.18362.836]
ver took 0:0:0.00 (0.00s total)
type nul took 0:0:0.00 (0.00s total)
VERIFY is off. VERIFY took 0:0:0.00 (0.00s total)
This took 0:0:0.06 (0.06s total)
cmd /v:on /c set ^"q=^"^" & timeit.cmd "cmd /c ^!q^!exit /b 0^!q^!" "cd ." "ver" "type nul" “核实”
cmd /c "exit /b 0" 花费了 0:0:0.02(总共 0.02s)
光盘。花费了 0:0:0.00(总共 0.00 秒)
Microsoft Windows [版本 10.0.18362.836]
ver 花费了 0:0:0.00(总共 0.00 秒)
类型 nul 花费了 0:0:0.00(总共 0.00 秒)
验证关闭。VERIFY 耗时 0:0:0.00(总共 0.00 秒)
这花了 0:0:0.06(总共 0.06 秒)
after reviewing with Measure-Command {command}
in Powershell, I found that it only really accepted cd .
and cmd /c "exit /b 0"
--am I doing something wrong?
与后Measure-Command {command}
在PowerShell中,我发现它只有真正接受cd .
和cmd /c "exit /b 0"
--am我做错了什么?
I'd recommend either cd .
or type nul
since neither have a footprint on the output of the console, nor are they slow in any measure.
我建议cd .
或者type nul
因为在控制台的输出上都没有足迹,而且它们在任何方面都不慢。
yeah I'm pretty bored
是的,我很无聊
回答by Chiefy
Add >nul
after each command that's likely to fail - this seems to prevent the build from failing.
>nul
在每个可能失败的命令之后添加- 这似乎可以防止构建失败。
You can still check the result of the command by examining %errorlevel%
.
您仍然可以通过检查来检查命令的结果%errorlevel%
。
For example:
例如:
findstr "foo" c:\temp.txt>nul & if %errorlevel% EQU 0 (echo found it) else (echo didn't find it)