Search code examples
nant

How do I check if a file contains a string (using Nant)?


At the moment I'm calling findstr and storing the output in a property to check afterwards - I'm sure there must be a better solution.

<exec program="findstr.exe"
    workingdir="${workspaceDir}"
    commandline='/i /c:"someText" ${fileName}'
    failonerror="false"
    output="${coverageExcludeLog}"
    resultproperty="foundFile"
/>

Is this really the best way of doing this?


Solution

  • <loadfile file="${fileName}" property="MyFileContents" />
    
    <property name="Mystring" value="someText" />
    
    <property name="search.file" value="${string::contains(MyFileContents, Mystring)}" />
     <if test="${bool::parse(search.file)}" > <!-- true or false-->
      <echo message="Found the  string ${Mystring} in the file ${fileName}" />
     </if>