エラーレベルをゼロにリセットする最も簡単な方法は何ですか?
-
12-09-2019 - |
質問
AC#プロジェクトのコマンドを実行するビルド後のイベントがあります。最後のコマンドは、エラーレベルの値がゼロに等しくなく、ビルドが失敗する場合があります。
エラーレベルの値を常にゼロに設定するために、追加のコマンドラインを追加したいと思います。それを行うための最も便利な方法は何ですか?
解決 4
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.
他のヒント
使用する場合 exit /b 0
あなたは返すことができます errorlevel 0
親を終了することなく、子バッチスクリプト内から。
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
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:
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.
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.
I use VERIFY
or VERIFY > nul
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.
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)
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
For the sake of completeness, this sets the ErrorLevel
state to 1
, valid for both Windows and MS-DOS:
< nul find ""
I'm using this:
ping localhost -n 1 >null
I always just used;
set ERRORLEVEL=0
I've been using it for donkey's years.