Cmd msbuild errorlevel always 0

I have this code in the 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% is always 0, even if there are errors and warnings in the log file.

+6
source share
2 answers

JozefZ's comment helped me:

Use SETLOCAL EnableDelayedExpansion and! errorlevel! instead of% errorlevel% or (better), return to IfLevelLevel 1 syntax. Setting EnabledDelayedExpansion will expand each variable at runtime rather than parsing: parsing, the command interpreter evaluates the line-by_line and / or command_by_command variables, but the entire code block in () parentheses counts as one command. On the other hand, if ErrorLevel 1 should be read as if ErrorLevel is greater than or equal to 1, which is not equal to 0

+5
source

I had to figure it out a bit, but it helped me:

 msbuild mySolution.sln if errorlevel 1 exit /b errorlevel 

I did not need to use SETLOCAL EnableDelayedExpansion

0
source

Source: https://habr.com/ru/post/979335/


All Articles