MSbuild Task FileUpdate to replace content in text files

I wanted to replace some strings in files using my deployment MSbuild script.

I've noticed that MSBuild Community Tasks Project has RegexReplace task.

But when I've looked in documentation 

(By the way, it will be good if Reference help will be available online, not only from download)

I've realized that the task is applicable for strings(e.g file names) not to content within a file.

Almost accidently in one of the posts i've found a reference to FileUpdate
task, that support Regex and does content replacements within a file.

The following examle (from downloaded help) search for a version number and update the revision.

   
            <FileUpdate Files="version.txt"
                Regex="(\d+)\.(\d+)\.(\d+)\.(\d+)"
                ReplacementText="$1.$2.$3.123" />

Note: don't forget to insert in to your project

<MSBuildCommunityTasksPath>.</MSBuildCommunityTasksPath>
<Import Project="MSBuild.Community.Tasks.Targets"/>

You may require to change MSBuildCommunityTasksPath if it is not in current directory.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Friday, July 03, 2009 8:45 PM
Print

Comments on this entry:

# re: MSbuild Task FileUpdate to replace content in text files

Left by Rocky at 12/16/2010 10:11 AM
Gravatar
Hey I get an Object Reference not set to an instance of an object error when I use the below code. The error is thrown at the FileUpdate statement:

<Target Name="AfterGet">
<!-- Update all the assembly info files with generated version info -->
<Message Text="Modifying AssemblyInfo files under &quot;$(SolutionRoot)&quot;." />
<Attrib Files="@(AssemblyInfoFiles)" Normal="true" />
<FileUpdate Files="@(AssemblyInfoFiles)"
Regex="AssemblyVersion\(&quot;.*&quot;\)\]"
ReplacementText="AssemblyVersion(&quot;$(VersionMajor).$(VersionMinor).$(VersionService).$(VersionBuild)&quot;)]" />
<FileUpdate Files="@(AssemblyInfoFiles)"
Regex="AssemblyFileVersion\(&quot;.*&quot;\)\]"
ReplacementText="AssemblyFileVersion(&quot;$(VersionMajor).$(VersionMinor).$(VersionService).$(VersionBuild)&quot;)]" />
<Message Text="AssemblyInfo files updated to version &quot;$(VersionMajor).$(VersionMinor).$(VersionService).$(VersionBuild)&quot;" />
</Target>

Any help will be highly appreciated!!!

# Error Resolved

Left by Rocky at 12/17/2010 10:08 AM
Gravatar
Hi, I resolved the error updating the FileUpdate statements as:

<FileUpdate Files="@(AssemblyInfoFiles)"
Regex="AssemblyVersion\(\&quot;(\d+.\d+.\d+.\d+)\&quot;\)" ignoreCase="true"
ReplacementText="AssemblyVersion(&quot;$(VersionMajor).$(VersionMinor).$(VersionService).$(VersionBuild)&quot;)" />
<FileUpdate Files="@(AssemblyInfoFiles)"
Regex="AssemblyFileVersion\(\&quot;(\d+.\d+.\d+.\d+)\&quot;\)" ignoreCase="true"
ReplacementText="AssemblyFileVersion(&quot;$(VersionMajor).$(VersionMinor).$(VersionService).$(VersionBuild)&quot;)" />

# So disgusting...

Left by GoatmasterX at 4/20/2011 9:39 AM
Gravatar
...but it works, where nothing else would.

# re: MSbuild Task FileUpdate to replace content in text files

Left by Ullas at 7/11/2011 12:48 PM
Gravatar
I also get the Object reference error but i get it only when it is run via TFS 2010 but if i run it standalone i do not get this error. Any ideas?

# re: MSbuild Task FileUpdate to replace content in text files

Left by 乐吧淘 at 1/7/2012 5:46 PM
Gravatar
i get it only when it is run via TFS 2010 but if i run it standalone i do not get this error. 乐吧淘Any ideas?

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910