Renaming Bunch of Files using MSBuild

I need to rename bunch of Files by using MSBuild. Can anyone suggest any idea ?

This is my requirement..

  1. Test1_20140415_Next.txt
  2. Test2_20140415_Next.txt
  3. Test3_20140415_Next.txt

Above files should be renamed as

  1. Test1_20140416_Next.txt
  2. Test2_20140416_Next.txt
  3. Test3_20140416_Next.txt

Just replacing the date.

Thanks in Advance.

Upvotes: 0

Views: 1246

Answers (1)

granadaCoder
granadaCoder

Reputation: 27852

This works. I am importing 2 libraries.

I actually get today's date...not a hard coded one.

<?xml version="1.0" encoding="utf-8"?>
<Project  ToolsVersion="4.0"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapped">

    <Import Project="$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks"/>
    <Import Project="$(MSBuildExtensionsPath32)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />

    <PropertyGroup>
        <!-- Always declare some kind of "base directory" and then work off of that in the majority of cases  -->
        <WorkingCheckout>.</WorkingCheckout>
    </PropertyGroup>

    <Target Name="AllTargetsWrapped">
        <CallTarget Targets="SetTodayProperty" />
        <CallTarget Targets="CopyWithNewNameThenDeleteFiles" />
    </Target>   

    <Target Name="SetTodayProperty">
        <Time Format="yyyyMMdd">
            <Output TaskParameter="FormattedTime" PropertyName="UniqueDateStampTag" />
        </Time>
    </Target>


    <Target Name="CopyWithNewNameThenDeleteFiles">
        <ItemGroup>
            <AllFiles Include="$(WorkingCheckout)\MySourceFolder\**\*.*" />
        </ItemGroup>
        <MSBuild.Community.Tasks.RegexMatch Expression="20140415_Next\.txt$" Input="%(AllFiles.FullPath)">
            <Output TaskParameter="Output" ItemName="MatchedFiles" />
        </MSBuild.Community.Tasks.RegexMatch>

        <MSBuild.ExtensionPack.Framework.TextString TaskAction="Replace" NewValue="$(UniqueDateStampTag)" OldString="%(MatchedFiles.FullPath)" OldValue="20140415">
            <Output TaskParameter="NewString" ItemName="NewFiles" />
        </MSBuild.ExtensionPack.Framework.TextString>

        <Copy SourceFiles="@(MatchedFiles)" DestinationFiles="@(NewFiles)" />
        <Delete Files="@(MatchedFiles)" />
    </Target>

</Project>

Upvotes: 3

Related Questions