使用MSBuild重命名文件束

时间:2014-04-18 09:26:01

标签: msbuild

我需要使用MSBuild重命名一堆文件。任何人都可以提出任何想法吗?

这是我的要求..

  1. Test1_20140415_Next.txt
  2. Test2_20140415_Next.txt
  3. Test3_20140415_Next.txt
  4. 以上文件应重命名为

    1. Test1_20140416_Next.txt
    2. Test2_20140416_Next.txt
    3. Test3_20140416_Next.txt
    4. 只需更换日期。

      先谢谢。

1 个答案:

答案 0 :(得分:3)

这很有效。我正在导入2个库。

我实际上得到了今天的约会......而不是硬编码的。

<?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>