tags:

views:

775

answers:

6

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

Thanks Maddy

+1  A: 
<?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>
Binoj Antony
Thanks a lot binoj.Just got a another doubt??I am really new to nant.So I have a value in this file.txt which is read as seen from ur code above,now i want to compare it with a fixed value and look for validation.If this value is greater or lesser i wann do some checks.Can u plz help??
Maddy
<if test="${file.content=='someValue'}"> should be it
Binoj Antony
A: 

Thanks a lot binoj.Just got a another doubt??I am really new to nant.So I have a value in this file.txt which is read as seen from ur code above,now i want to compare it with a fixed value and look for validation.If this value is greater or lesser i wann do some checks.Can u plz help??

Thanks Maddy

Maddy
I have updated the answer.
Binoj Antony
A: 

Thanks for that valuable updates.my file.txt contains data as follows

Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

3

i really need the value 3 to be read .I dont want the whole contents written above to be read.How should i filter it and see?The code above which u had given reads the whole file.

Thanks Maddy

Maddy
Put the full script to read the 3rd line!
Binoj Antony
Hi Binoj,Thanks a lot for the soultion.But when i had executed this nant script,i had got this shown belowmain: [echo] found at line 3BUILD SUCCEEDEDTotal time: 0.3 seconds.Infact it is not printing the value as needed.Can u plz help??
Maddy
A: 

Hi Binoj,Thanks a lot for the soultion.But when i had executed this nant script,i had got this shown below main: Target framework: Microsoft .NET Framework 2.0 Target(s) specified: main

main:

 [echo] found   at line 3

BUILD SUCCEEDED

Total time: 0.3 seconds.

Infact it is not printing the value as needed.Can u plz help??

Maddy
A: 

Binoj, Its working correctly.Thanks a lot.Plz ignore the previous question.

Thanks Maddy

Maddy
A: 

Hi Binoj,

How can I read a value alone from the property file. For example, how can I read the values of build.home or temp.dir from the below example.

contents of env.properties

build.home=c:\build temp.dir=c:\temp

Regards

Sarathy