The case of the failing ant copy filterset

I wanted to set a couple of Windows environment variables from Ant. The environment key-value pairs are stored on the HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment key in the registry. As a starting point I used the registry editor to export all keys. I called the file environment.reg.template and modified it to look something like this:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
"FOO"="@FOO"
"BAR"="@BAR@"

My ant script contains something like this:

<copy file="environment.reg.template" tofile="environment.reg"
    overwrite="true">
  <filterset>
    <filter token="FOO" value="${foo}"/>
    <filter token="BAR" value="${bar}"/>
  </filterset>
</copy>
<exec executable="regedit.exe">
  <arg value="/s"/><!-- "silent" -->
  <arg value="environment.reg"/>
</exec>
 

I ran the ant script and looked in environment.reg. It still looked just like environment.reg.template! Running again obviously didn’t work, but I had to try! 🙂 When did a cut & paste of the above and sent to a colleague he could not reproduce the issue. I thought hard about it and then it dawned on me: the registry editor exported a file in Unicode format. I used notepad to convert it to ANSI and then everything worked fine. Case closed.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.