2009-04-10 70 views

回答

12
<?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> 

當然,如果你願意,你可以跳過線6,9和10。 [編輯]

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

this link

[EDIT2]

獲取全部細節既然你想要得到的文本文件中的第三行的值,那麼做到這一點

<?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> 
+0

多謝內(?) binoj.Just得到了另一個疑問??我對nant真的很陌生。因此,我在這個file.txt中有一個值,它是從上面的代碼中看到的,現在我想將它與一個固定值進行比較並查找驗證。如果這個值大或小,我想做一些checks.CanüPLZ幫助? – Maddy 2009-04-10 12:45:36

0

我發現使用正則表達式更加靈活,因爲它不依賴於一行來處於特定的位置並且更易於編碼。

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

的語法是有點奇怪,但2號線的結果是填充屬性調用assemblyVersion與正則表達式的分組模式相匹配的字符串:一切