Search code examples
cmdmsbuilderrorlevel

cmd msbuild errorlevel is always 0


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.


Solution

  • 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