Ever needed to search a bunch of files to see if they contain a certain text fragment? Well I did, and now I needed to do this from a build script so the Windows Explorer was no option. At first I looked into using a windows port of grep, but since I preferred to have XML output I decided to write a small custom task for NAnt to do this.

The code for this is very trivial; iterate through a list of files and search them for matches to a regular expression. For each encountered match, an element is added to the resulting XML file.

To use this task, simply unzip this archive into the NAnt bin directory.

You can now use the regexfinder tag. If you wanted to scan all .cs files under C:\Projects\MySources\ (including all subdirectories) for todo’s and export the result to c:\todos.xml, you’d just add a target like this to your build file:

<target name="find_todos">
  <regexfinder pattern="//\s*TO\s*DO.*" 
               outputfile="c:\todos.xml" 
               ignorecase="true">
    <fileset basedir="C:\Projects\MySources">
      <include name="**/*.cs"/>
    </fileset>
  </regexfinder>
</target>

This will produce an XML file containing the filename, line number and complete line text for each match.

You can also just download the source code for this task if you like. To compile it, just add it to a project, reference the NAnt.Core dll and complile it.

If you don’t have NAnt yet, it is freely available from the NAnt website at SourceForge; or just download it directly using this link.