Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nant scripts written to read a value from a text file

Tags:

.net

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

Thanks Maddy

like image 509
Maddy Avatar asked Dec 17 '22 08:12

Maddy


1 Answers

<?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>
like image 91
Binoj Antony Avatar answered Jan 05 '23 23:01

Binoj Antony