将 ERRORLEVEL 重置为零的最简单方法是什么?

发布于 2024-07-26 16:35:54 字数 129 浏览 4 评论 0原文

我有一个构建后事件,为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

作死小能手 2024-08-02 16:35:54

如果您使用 exit /b 0,您可以从子批处理脚本中返回 errorlevel 0,而无需退出父批处理脚本。

if you use exit /b 0 you can return an errorlevel 0 from within a child batch script without also exiting the parent.

悍妇囚夫 2024-08-02 16:35:54

似乎成功了:

ver > nul

并非一切都有效,而且还不清楚原因。 例如,以下情况则不然:

echo. > nul
cls > nul

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
亽野灬性zι浪 2024-08-02 16:35:54

在构建前或构建后事件中,如果可执行文件的返回代码大于零,并且对可执行文件的调用不是构建前或构建后事件的最后一行,则可以快速将其静音并避免触发非零 errorlevel 检查的方法是在失败的行后面加上显式返回零的行:

cmd /c "exit /b 0"

这本质上是前面提到的解决方案的通用组合,适用于超过 < 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:

cmd /c "exit /b 0"

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.

自找没趣 2024-08-02 16:35:54

我个人使用这个:

cd .

即使在 unix shell 中也可以工作。

但是,这个可能会快一点:

type nul>nul

因为 Process Monitor 显示 cd 上的 QueryDirectory 调用。

附注:
cd . 在 unix shell 中还有另一个很好的副作用。 如果在擦除之前已打开工作目录,它会恢复终端中重新创建的工作目录。

更新

这更快一点:

call;

有任何 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 shows QueryDirectory calls on 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.

Update:

And that is a bit more faster:

call;

Any windows command to find if a file is in use or not ? :
https://www.dostips.com/forum/viewtopic.php?t=5542

坠似风落 2024-08-02 16:35:54

我发现“exit 0”看起来是解决这个问题的好方法。

使用示例:

NET STOP UnderDevService /Y

退出0

如果未启动 UnderDevService 服务,则

I found that "exit 0" looks like a good way to deal with this problem.

Usage Example:

NET STOP UnderDevService /Y

exit 0

if the UnderDevService service is not started.

我喜欢麦丽素 2024-08-02 16:35:54

我使用 VERIFYVERIFY > 空

I use VERIFY or VERIFY > nul

内心激荡 2024-08-02 16:35:54

如果这是像“构建后事件”等这样的片段,那么您可以

(...) || ver > nul

在最后一个命令的末尾附加:。

另一种方式

cmd /c "exit /b 0"

非常干净且非惯用——了解 Windows shell 的读者会知道发生了什么以及您的意图是什么。

但是,如果您使用批处理脚本,则可能需要使用子程序,它是 akf 答案中的“子批处理脚本”的轻量级等效项。

有一个子例程:

:reset_error
exit /b 0

然后

call :reset_error

在您需要的地方使用它。

这是一个完整的示例:

@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

哪个输出:

After :raise_error ERRORLEVEL = 1
After :empty ERRORLEVEL = 1
After :reset_error ERRORLEVEL = 0

如您所见 - 仅通过 goto:eof 调用并返回是不够的。

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.

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:

: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.

临风闻羌笛 2024-08-02 16:35:54

以下内容适用于具有 cmd.exe 的现代 Windows(基于 NT)系统:

rem /* This clears `ErrorLevel`; the SPACE can actually be replaced by an
rem    arbitrary sequence of SPACE, TAB, `,`, `;`, `=`, NBSP, VTAB, FF: */
(call )

SPACE(或更准确地说,一个或多个标准标记分隔符的任意序列,分别是 SPACE(代码 0x20)、TAB(代码 0x09)、 , ;, =, NBSP (代码 0xFF),VTAB (代码 <代码>0x0B)和FF(代码0x0C))是强制性的; 如果省略它,则设置 ErrorLevel

rem // This sets `ErrorLevel` to `1`:
(call)

DosTips.com 出现了这种技术。


这是另一种方法,但它访问文件系统,因此可能会慢一些:

dir > nul

rem /* Perhaps this is a little faster as a specific file is given rather 
rem    than just the current directory (`.` implicitly) like above: */
dir /B "%ComSpec%" > nul

The following works in modern Windows (NT-based) systems that feature cmd.exe:

rem /* This clears `ErrorLevel`; the SPACE can actually be replaced by an
rem    arbitrary sequence of SPACE, TAB, `,`, `;`, `=`, NBSP, VTAB, FF: */
(call )

The SPACE (or more precisely, an arbitrary sequence of one or more standard token separators, which are SPACE (code 0x20), TAB (code 0x09), ,, ;, =, NBSP (code 0xFF), VTAB (code 0x0B) and FF (code 0x0C)) is mandatory; if you omit it the ErrorLevel becomes set instead:

rem // This sets `ErrorLevel` to `1`:
(call)

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:

dir > nul

rem /* Perhaps this is a little faster as a specific file is given rather 
rem    than just the current directory (`.` implicitly) like above: */
dir /B "%ComSpec%" > nul
迷荒 2024-08-02 16:35:54

以下是重置 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

以下方法仅适用于 MS-DOS:

command /? > nul

fc nul nul > nul

keyb > nul

为了完整起见,这将 ErrorLevel 状态设置为 1,对 Windows 和 MS-DOS 均有效:

< nul find ""

Here are some other ways to reset the ErrorLevel state, which even work in MS-DOS (at least for version 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:

command /? > nul

fc nul nul > nul

keyb > nul

For the sake of completeness, this sets the ErrorLevel state to 1, valid for both Windows and MS-DOS:

< nul find ""
此岸叶落 2024-08-02 16:35:54

在查看了所有其他答案后,我决定找出哪种方法重置错误级别最有效。 我制作了一个快速脚本,记录了执行每个脚本的时间:

"cmd /c "exit /b 0"", "cd .", "ver", "type nul", and "VERIFY"

这是输出:

cmd /v:on /c set ^"q=^"^" & timeit.cmd "cmd /c ^!q^!exit /b 0^!q^!" "cd ." "ver" “类型nul”“验证”

cmd /c“exit /b 0”花费了 0:0:0.02(总共 0.02 秒)

光盘。 花费了 0:0:0.00(总共 0.00 秒)

Microsoft Windows [版本 10.0.18362.836]

版本耗时 0:0:0.00(总共 0.00 秒)

输入 nul 花了 0:0:0.00(总共 0.00 秒)

验证已关闭。
验证花费了 0:0:0.00(总共 0.00 秒)

<小时>

这花了 0:0:0.06(总共 0.06 秒)

,我发现它只真正接受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:

"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)

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?

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.

yeah I'm pretty bored

可可 2024-08-02 16:35:54

在每个可能失败的命令后面添加 >nul - 这似乎可以防止构建失败。

您仍然可以通过检查 %errorlevel% 来检查命令的结果。

例如:

findstr "foo" c:\temp.txt>nul & if %errorlevel% EQU 0 (echo found it) else (echo didn't find it)

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:

findstr "foo" c:\temp.txt>nul & if %errorlevel% EQU 0 (echo found it) else (echo didn't find it)
于我来说 2024-08-02 16:35:54

用户 aschipfl 给出了最佳答案,使用最小的函数 FIND 来完成它:

此行将在 DOS 或更高版本中将 ERRORLEVEL 设置为 1,在 DOS 或更高版本中:

<nul find ""
if errorlevel 1 echo ERRORLEVEL is 1 or more

此行将在 DOS 或更高版本中将 ERRORLEVEL 设置为 0:

echo a |find "a" >nul
if errorlevel==0 echo No Error

User aschipfl gave best answer, to complete it, using smallest function FIND:

This line will set ERRORLEVEL to 1, in DOS or later:

<nul find ""
if errorlevel 1 echo ERRORLEVEL is 1 or more

This line will set ERRORLEVEL to 0, in DOS or later:

echo a |find "a" >nul
if errorlevel==0 echo No Error
枯叶蝶 2024-08-02 16:35:54

(重新)设置 %ERRORLEVEL% 的一个有趣选项是使用本地函数(这与 @akf 的建议类似):

@echo off
Something
SomethingElse
...
call :SetErrorLevel 0
goto :eof

rem Local function
:SetErrorLevel DesiredErrorLevel
exit /b %~1

尽管将 ERRORLEVEL 重置为 0(表示成功)是最有效的常见用法,您可以使用它来设置任何特定错误。

One interesting option to (re)set %ERRORLEVEL% is to use a local function (this would be similar to the suggestion from @akf):

@echo off
Something
SomethingElse
...
call :SetErrorLevel 0
goto :eof

rem Local function
:SetErrorLevel DesiredErrorLevel
exit /b %~1

Although resetting ERRORLEVEL to 0 (indicating success) would be the most common usage, you could use this to set any specific error.

蓝眼泪 2024-08-02 16:35:54

尝试了上述所有解决方案 - 没有任何效果。 但在批处理文件中找到了适合我的解决方案:

set /A errorlevel=0

(/A 开关将提供的值定义为数字表达式)

Tried all solutions above - nothing worked. But found this solution that works for me inside the batch file:

set /A errorlevel=0

(the /A switch defines the supplied value as a numerical expression)

心不设防 2024-08-02 16:35:54

我正在使用这个:

ping localhost -n 1 > null

I'm using this:

ping localhost -n 1 >null

坏尐絯 2024-08-02 16:35:54

我总是只是用;

set ERRORLEVEL=0

我已经使用它很多年了。

I always just used;

set ERRORLEVEL=0

I've been using it for donkey's years.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文