Values ​​written for reading from a text file

Can someone send me an example nant.build file that reads a value from a text file named file.txt.

Thanks Maddy

+4
source share
2 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. [Edit]

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

Get full information at this link

[EDIT2]

Since you want to get the value of the third line of a text file, 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> 
+12
source

I found that using a regular expression is more flexible because it does not rely on a string that is in a specific place and is easier to encode.

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

The syntax is a bit odd, but the result of the second line is to populate the assemblyVersion property call with a string, matched by a regular expression grouping pattern: everything inside (?)

0
source

All Articles