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.

posted @ Friday, July 3, 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?

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

Left by lucalla at 7/19/2012 6:53 AM
Gravatar
How do I get Regex to ignore the case on the ReguarExpression attribute as shown below?


<File.RegEx
Path="\\$(DBServer)\c$\Projects\Projects\$(sourceBranch)\__Database\$(SSISSchoolYear)\SSIS\File1"
Force="true"
NewValue="C:\Projects\Projects\$(sourceBranch)\__Database\$(SSISSchoolYear)\SSIS\filex"
RegularExpression="C:\\projects\\Projects\\$(sourceBranch)\\DEV\\__Database\\$(SSISSchoolYear)\\SSIS\\Filey">
</File.RegEx>

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

Left by michael freidgeim at 7/21/2012 10:27 PM
Gravatar
Add ignoreCase="true". See example in the previous comment of Rocky.

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

Left by Vinay at 4/25/2013 10:00 PM
Gravatar
Can you please mention the a sample project, i.e., where do you add this and how do you create a prebuild replacement of these files. a sample ccnet.config file would hlep.

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

Left by Jonny Cundall at 9/19/2013 8:09 PM
Gravatar
Thanks, Michael, this fixed my issue, getting round a limitation of my wix/msbuild scripts

Your comment:



(not displayed)

 
 
 
 

Live Comment Preview:

 
«December»
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910