How to replace string in a file using NANT?

RKM picture RKM · Dec 19, 2011 · Viewed 9.6k times · Source

I am trying to replace the occurance of a string in a wxs file using Nant.

I have only found the following example, which uses <replaceString>, but it seems like it can only be used within the copied files. Are there any other way of replacing a string, without actually copying the files over?

<property name="NOW" value="${datetime::now()}" />
<copy todir="out">
    <fileset basedir="in">
        <include name="**/*" />
    </fileset>
    <filterchain>
        <replacetokens>
            <token key="NOW" value="${TODAY}" />
        </replacetokens>
        <tabstospaces />
    </filterchain>
</copy>

Answer

Yan Sklyarenko picture Yan Sklyarenko · Dec 21, 2011

Here's the code:

<loadfile file="token.txt" property="token-file">
    <filterchain>
        <replacetokens>
            <token key="NOW" value="${datetime::now()}" />
        </replacetokens>
    </filterchain>
</loadfile>

The official NAnt docs for <loadfile> element contain the exact sample you need. See the bottom of the page.