Geeks With Blogs
Sudheer Kumar ASP.Net, C#, BizTalk, MSBuild, WPF, WCF, WF....
Another MSBuild lesson.
 You can make batch file return an error code to a MSBuild Task and the custom error codes can be handles as mentioned in the previous post.

<Target Name="AfterCompile">

        <Exec Command='"C:\Temp\Test.bat"' ContinueOnError='true'>
            <Output TaskParameter="ExitCode" PropertyName="ErrorCode"/>
        </Exec>
        <Message Text="The exit code is $(ErrorCode)"/>
        <Error Text="Error while executing BAT file" Condition="'$(ErrorCode)' > '0'" />
        <OnError ExecuteTargets="MessageErrorHandler"/>
  </Target>
    
  <!--Error Handler-->
  <Target Name="MessageErrorHandler">
        <Message Text="An error has occurred while running the batch file"/>
  </Target>
   

And my batch file looks like:

@ECHO OFF
"C:\Temp\ConsoleApplication1.exe"
if ERRORLEVEL 1 exit 1
if ERRORLEVEL 0 exit 0

or
exit ERRORLEVEL



Posted on Friday, October 19, 2007 4:30 PM MSBuild | Back to top


Comments on this post: Batch File Returning Values to MSBuild

# re: Batch File Returning Values to MSBuild
Requesting Gravatar...
thanku...
It is really good.This helped me.This was that i wanted.Thanks again..
Left by Anand on Mar 02, 2009 10:45 PM

# re: Batch File Returning Values to MSBuild
Requesting Gravatar...
So here is something strange... If i open a new command prompt window...

C:\works>echo %ERRORLEVEL%
0

C:\works>msbuild xxx
Microsoft (R) Build Engine Version 2.0.50727.1433
[Microsoft .NET Framework, Version 2.0.50727.1433]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

MSBUILD : error MSB1009: Project file does not exist.
Switch: xxx

C:\works>echo %ERRORLEVEL%
1

C:\works>set ERRORLEVEL=0

C:\works>echo %ERRORLEVEL%
0

C:\works>msbuild xxx
Microsoft (R) Build Engine Version 2.0.50727.1433
[Microsoft .NET Framework, Version 2.0.50727.1433]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

MSBUILD : error MSB1009: Project file does not exist.
Switch: xxx

C:\works>echo %ERRORLEVEL%
0

I noticed msbuild changed the ERRORLEVEL first time but not again. Am i doing the right thing to capture the errorlevel? How can i capture msbuild errorlevel correctly in command line and in batch file?

Thanks. B.
Left by bazthemail on Jul 01, 2009 12:30 PM

# re: Batch File Returning Values to MSBuild
Requesting Gravatar...
May be i am not setting errorlevel correctly?
Left by baztheman on Jul 01, 2009 12:44 PM

# re: Batch File Returning Values to MSBuild
Requesting Gravatar...
If you "set ERRORLEVEL=0" you'll create an environment variable called "ERRORLEVEL" which is set to 0. This will then mask your attempts to check the ERRORLEVEL with "%ERRORLEVEL%", which is why it looks like msbuild isn't setting the ERRORLEVEL anymore.
Left by psommer on Nov 11, 2009 3:57 PM

Your comment:
 (will show your gravatar)


Copyright © Sudheer Kumar | Powered by: GeeksWithBlogs.net