cmd msbuild errorlevel is always 0
Asked Answered
W

2

11

I have this code in my build.bat file

for /R %~dp0 %%A In (*.sln) do (
c:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe %%A /t:rebuild /nologo /verbosity:minimal /flp:Verbosity=detailed;LogFile=%~dp0\Logs.txt;append=true /m /p:Configuration=Debug;Platform="Any CPU" /p:VisualStudioVersion="12.0"
if not %errorlevel%==0 set Failed+=1
pause)

My problem is that %errorlevel% always 0 even when log file have errors and warnings.

Writhen answered 9/12, 2014 at 15:31 Comment(5)
For the failing cases what is the value of ERRORLEVEL before your condition i.e. could you temporarily add ECHO %ERRORLEVEL% before your if statement.Crosshead
Use either SETLOCAL EnableDelayedExpansion and !errorlevel! instead of %errorlevel% or (better) return to If ErrorLevel 1 syntax. Setting EnabledDelayedExpansion will cause each variable to be expanded at execution time rather than at parse time: parsing, the command interpreter evaluates variables line-by_line and/or command_by_command but all code block in () parentheses considers to be one command. On the other hand, If ErrorLevel 1 should to be read as if ErrorLevel is greater than or equal to 1 thus not equal to 0 (zero)Matter
Thanks for your reply, but I still have issue, as I see always 0 in errorlevel. Can you please provide final version of my code as answer?Writhen
Can someone provide me final version of code? I think that I already tested all possible variants but no results.Writhen
So I have no working variant, please help!Writhen
W
6

Comment of JozefZ helped me:

Use either SETLOCAL EnableDelayedExpansion and !errorlevel! instead of %errorlevel% or (better) return to If ErrorLevel 1 syntax. Setting EnabledDelayedExpansion will cause each variable to be expanded at execution time rather than at parse time: parsing, the command interpreter evaluates variables line-by_line and/or command_by_command but all code block in () parentheses considers to be one command. On the other hand, If ErrorLevel 1 should to be read as if ErrorLevel is greater than or equal to 1 thus not equal to 0

Writhen answered 23/3, 2015 at 16:40 Comment(0)
L
5

Took me a bit to sort this out but this is what worked for me:

msbuild mySolution.sln
if errorlevel 1 exit /b errorlevel

I did not need to use SETLOCAL EnableDelayedExpansion

Lemures answered 9/3, 2018 at 18:54 Comment(1)
Simplest option!Onitaonlooker

© 2022 - 2024 — McMap. All rights reserved.