Pages Menu

Posted by on Sep 28, 2010 in Development | 0 comments

NAnt task – bat file error codes

Just a quick something I found out a couple of moments ago.

The <exec> task in NAnt is a life-saver for gluing together functionality that’s locked up in other programs, or in existing batch files. For a number of reasons you may want to keep some functionality in existing batch files, and call them from NAnt.

However, sometimes the batch files can go wrong, and you want a way of reacting. Well, MS-DOS batch files in Windows 2000/XP and later can return error codes, using this line:


where n is the error code you want to use.

[There is an optional /B flag on the EXIT command, this is for when you want to exit the batch file with the code rather than CMD.EXE. If you do this when calling the batch file from NAnt then the batch file will exit to CMD.EXE with the return code, but CMD.EXE will then exit to NAnt with a normal (0) error code, thus defeating the point of the exercise.]

When called from NAnt, anything other than returning 0 will fail the build.

If you’re getting really complicated, and want to do different things depending on where you exited the batch file, set up different return codes. In NAnt, in the <exec> task, turn off failonerror but specify that the return code should go to a variable, like this:

<exec program="test.bat"

Now you can evaluate the property and take action based on the return code.

Post a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This site uses cookies to help make this website better. By continuing to use this site we’ll assume you’re OK with that (implied consent).