How to replace string in a file using NANT?
Asked Answered
C

7

16

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>
Chaconne answered 19/12, 2011 at 15:17 Comment(0)
Q
10

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.

Quar answered 21/12, 2011 at 20:29 Comment(1)
this almost worked for me, what it was missing was echo'ing the file back out to replace the old one - #487951Gentlewoman
D
9

Here's how I did it.

<loadfile file="${file}" property="file.content">
    <filterchain>
        <replacestring from="StringToMatch" to="StringToReplace" ignorecase="true" />
    </filterchain>
</loadfile>
<echo file="${file}">${file.content}</echo>
Disavowal answered 13/9, 2013 at 11:1 Comment(0)
S
3

So you are trying to modify a .wxs file which is XML, right?

In this particular case you might use <xmlpoke> if you are able to determine the position of the strings to replace via XPath.

Scirrhous answered 22/12, 2011 at 9:38 Comment(0)
S
2

I found a solution for you here: http://frank.overseakids.com/?p=182

<loadfile file=”${dir.template}\template.db_name.sql” property=”restore.db.sql.db_name”>
<filterchain>
<replacetokens>
<!– this looks for tokens like @blah.blah@ in the file being loaded and replaces them–>
<token key=”restore.db.prefix” value=”${restore.db.prefix}” />
<token key=”backup.file.path” value=”${backup.file.path}” />
</replacetokens>
</filterchain>
</loadfile>
<property name=”current.db” value=”db_name” />
<property name=”current.log” value=”${dir.log}\${restore.db.logfile.prefix}_db_name.log” />
<property name=”current.file” value=”${dir.template}\restore.db_name.tmp.sql” />
<delete if=”${file::exists(current.file)}” file=”${current.file}” />
<echo file=”${current.file}”>${restore.db.sql.db_name}</echo>

You can wrap this in a <foreach /> element.

Steal answered 21/12, 2011 at 16:38 Comment(0)
A
1

I never managed to get the filterchain and replacetokens to work properly. I ended up using this and it works great.

<replacetext filename="${filename}" src="stringToBeReplaced" replacement="replacementString" />
Affirmation answered 4/12, 2012 at 20:28 Comment(0)
C
1

All these answers did not work for me, maybe because I needed to replace a string with spaces in it. Loading a file content with filterchain/replacetokens did nothing to the contents of the associated property. Maybe I'm using it wrong.
The tasks "replacestring" and "replacetext" suggested by @Ally and @John Sterne do not exist.

It's included in a Jenkins build process, thus the ENVIRONMENT variable must be set to the working dir.

    <loadfile file="./my/batch.bat" property="file.content" />
    <property name="file.content"
              value="${string::replace(file.content, 'D:\path to\the working\space', environment::get-variable('WORKSPACE'))}" />
    <property name="file.content"
              value="${string::replace(file.content, 'Cd C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin', 'CD /D C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin')}" />
    <echo file="./my/batch.bat">${file.content}</echo>
Chalcidice answered 12/12, 2013 at 10:46 Comment(0)
M
1

I had that problem today. To solve it I used the move command instead of loadfile or copy. This worked for me because since my file was pretty small. The other caution about this is that replacetokens needs a start identifier and end identifier of the token; begintoken and endtoke respectively. If those are not set the default values are the @ symbol. So if you want to replace a value such as MY_SERVER_PLACE_HOLDER that means the value in your file must be @MY_SERVER_PLACE_HOLDER@. If you want your token to start with something different than you should specify the begintoken and endtoken values. That should give you an idea of the problems the begin token and endtoken will bring you.

So here is what I did in a nutshell

  1. Moved the file to a temporary location. In that move I used filterchain with removetokens to change the values in the file.
  2. In step 2, I moved the file back to it's original location.
  3. I then used the delete command to delete the temp folder I created.

Here is a what I did. (May not be syntactically correct since I am not in front of the code at the moment)

    <move todir="temp">
       <fileset basedir="in">
         <include name="myfile.dat" />
       </fileset>
       <filterchain>
          <replacetokens>
             <token key="MY_SERVER_PLACE_HOLDER" value="http://www.someserver.com" />
          </replacetokens>
          <tabstospaces />
      </filterchain>
    </move>
    <move todir="in">
       <fileset basedir="temp">
           <include name="myfile.dat" />
       </fileset>
    </move>
    <delete dir="temp" />
Mosra answered 10/1, 2014 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.