Maddy
Maddy

Reputation: 141

nant scripts written to read a value from a text file

Can anyone send me a sample nant.build file which reads a value from a text file named file.txt.

Thanks Maddy

Upvotes: 4

Views: 7548

Answers (2)

raider33
raider33

Reputation: 1673

I found that using a regular expression is more flexible because it doesn't rely on a line to be in a specific location and is easier to code.

<loadfile file="${filename}" property="assemblyInfo" />
<regex input="${assemblyInfo}" pattern="(?'assemblyVersion'AssemblyVersion[0-9.()&quot;]+)" />

The syntax is a little odd, but the result of the 2nd line is to populate a property call assemblyVersion with the string matched by the regex grouping pattern: everything inside of (? )

Upvotes: 0

Binoj Antony
Binoj Antony

Reputation: 16204

<?xml version="1.0" encoding="utf-8" ?>
<project xmlns="http://nant.sf.net/release/0.86-beta1/nant.xsd" 
         name="Company.Portal.Domain" default="GetFile">
   <call target="GetFile" />
   <target name="GetFile">
     <echo message="Retrieving file contents"/>
     <property name="file.contents" value="0" />
     <loadfile file="file.txt" property="file.contents" />
     <property name="file.contents" value="${string::trim(file.contents)}" />
     <echo message="contents of file is  ${file.contents}"/>
   </target>
</project>

Of course you can skip lines 6, 9 and 10 if you want to. [Edit]

<if test="${file.contents=='someValue'}">
    <echo>Some value found</echo>
</if>

Get full details at this link

[EDIT2]

Since you want to get the value of the 3rd line of the text file then do this

<?xml version="1.0"?>
<project name="Read3rdLine" default="main">
    <property name="myInt" value="0"/>
    <property name="x" value="0"/>
    <property name="LineToRead" value="3"/>

    <target name="main" description="compiles the source code">
    <property name="i" value="0"/>
    <foreach item="Line" in="file.txt" property="x" trim="Both">
      <property name="i" value="${int::parse(i) + 1}"/>
      <if test="${i==LineToRead}">
          <property name="myInt" value="${x}"/>
      </if>
    </foreach>
    <echo>found  ${myInt} at line ${LineToRead}</echo>
    </target>
</project>

Upvotes: 12

Related Questions