Geeks With Blogs

Michael Freidgeim's Blog MS .Net Development

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 on Friday, July 3, 2009 8:45 PM Deployment | Back to top


Comments on this post: MSbuild Task FileUpdate to replace content in text files

# re: MSbuild Task FileUpdate to replace content in text files
Requesting 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!!!
Left by Rocky on Dec 16, 2010 10:11 AM

# Error Resolved
Requesting 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;)" />
Left by Rocky on Dec 17, 2010 10:08 AM

# So disgusting...
Requesting Gravatar...
...but it works, where nothing else would.
Left by GoatmasterX on Apr 20, 2011 9:39 AM

# re: MSbuild Task FileUpdate to replace content in text files
Requesting 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?
Left by Ullas on Jul 11, 2011 12:48 PM

# re: MSbuild Task FileUpdate to replace content in text files
Requesting 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?
Left by 乐吧淘 on Jan 07, 2012 5:46 PM

# re: MSbuild Task FileUpdate to replace content in text files
Requesting 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>
Left by lucalla on Jul 19, 2012 6:53 AM

# re: MSbuild Task FileUpdate to replace content in text files
Requesting Gravatar...
Add ignoreCase="true". See example in the previous comment of Rocky.
Left by michael freidgeim on Jul 21, 2012 10:27 PM

# re: MSbuild Task FileUpdate to replace content in text files
Requesting 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.
Left by Vinay on Apr 25, 2013 10:00 PM

# re: MSbuild Task FileUpdate to replace content in text files
Requesting Gravatar...
Thanks, Michael, this fixed my issue, getting round a limitation of my wix/msbuild scripts
Left by Jonny Cundall on Sep 19, 2013 8:09 PM

# re: MSbuild Task FileUpdate to replace content in text files
Requesting Gravatar...
I want to find text "Data Source=*;Initial Catalog=*;Persist" and replace it with Data Source=abc;Initial Catalog=def;Persist.
what will be the Regular Expression?
And moreover this task always results in success even when no changes to target file are done
Left by Ravneet on Aug 07, 2015 7:53 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net | Join free