我在文件夹中有一组文件。它们都具有与DR __。*模式匹配的名称。我想将它们复制到另一个文件夹,但删除DR__前缀。我怎么能用MSBuild做到这一点?我曾经这样使用NAnt:
<mkdir dir="${ClientPath + '\bin\' + ConfigurationName + '\Parameters'}"/>
<foreach item="File" property="Filename" in="CVParameters">
<if test="${string::contains(Filename, Client + '_')}">
<property name="newFilename" value="${ string::substring( Filename, string::last-index-of(Filename, '__') + 2, string::get-length(Filename) - string::last-index-of(Filename, '__') - 2) }"/>
<copy file="${ Filename }" tofile="${ ClientPath + '\bin\' + ConfigurationName + '\Parameters\' + newFilename }" overwrite="true"/>
</if>
</foreach>
答案 0 :(得分:14)
我同意@ Si的解决方案。但是使用MSBuild 4.0,您可以使用内置功能。 NAnt脚本比我的清晰得多。但我会将其添加为仅显示MSBuild 4.0技术的解决方案:
<ItemGroup>
<CVParameters Include="$(YourBaseDir)\**\DR__*" />
</ItemGroup>
<Target Name="CopyAndRename"
Condition="'@(CVParameters)'!=''"
Outputs="%(CVParameters.Identity)">
<PropertyGroup>
<OriginalFileName>%(CVParameters.FileName)%(CVParameters.Extension)</OriginalFileName>
<Prefix>DR__</Prefix>
<PrefixLength>$(Prefix.Length)</PrefixLength>
<OriginalFileNameLength>$(OriginalFileName.Length)</OriginalFileNameLength>
<SubstringLength>$([MSBuild]::Subtract($(OriginalFileNameLength),$(PrefixLength)))</SubstringLength>
<ModifiedFileName>$(OriginalFileName.Substring($(PrefixLength),$(SubstringLength)))</ModifiedFileName>
<DestinationFullPath>$([System.IO.Path]::Combine($(DestinationDir),$(ModifiedFileName)))</DestinationFullPath>
</PropertyGroup>
<Copy SourceFiles="%(CVParameters.FullPath)"
DestinationFiles="@(DestinationFullPath)"
SkipUnchangedFiles="true" />
</Target>
修改(按OP):要实现此功能,我必须将$(DestinationFullPath)
中的Copy
替换为@(DestinationFullPath)
,以匹配源文件和目标文件的数量。另外,我必须将前缀更改为DR__
,因为DR__.
无效。
答案 1 :(得分:10)
最近我不得不解决类似的任务,我使用Item的元数据来完成它。 该解决方案的优点是:
<TempItemsBak Include="@(TempItems-> ... CustomTransform ... )">
将获得原始项目的元数据值,以便您可以将其用于进一步处理) <Project DefaultTargets="CopyNoPrefix" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<InputDir Condition="'$(InputDir)' == '' ">.</InputDir>
<OutputDir Condition="'$(OutputDir)' == '' ">Output</OutputDir>
</PropertyGroup>
<ItemGroup>
<CVParameters Include="$(InputDir)\**\DR__.*" />
</ItemGroup>
<Target Name="CopyNoPrefix">
<ItemGroup>
<!-- Items with new name. Use System.String's Remove method to get full file name without prefix -->
<!-- http://msdn.microsoft.com/en-us/library/vstudio/ee886422(v=vs.100).aspx -->
<TempItems Include="@(CVParameters->'%(Filename)%(Extension)'->Remove(0, 5))">
<!-- Define metadata based on original item for next step -->
<OriginalPath>%(Identity)</OriginalPath>
<SavedRecursiveDir>%(RecursiveDir)</SavedRecursiveDir>
</TempItems>
</ItemGroup>
<!-- Use new items along with their metadata for copying -->
<Copy SourceFiles="@(TempItems->'%(OriginalPath)')" DestinationFiles="@(TempItems->'$(OutputDir)\%(SavedRecursiveDir)%(Identity)')" />
</Target>
</Project>
答案 2 :(得分:2)
您可以使用MSBuild 4.0吗?如果是,请参阅此answer(和MSDN help)。否则,MSBuildCommunityTasks中的RegexReplace任务也应该起作用,代价是必须支持外部工具(如果可能的话,请转到MSBuild 4.0)。
另一个(未经测试的)选项是TextString中的MSBuildExtensionPack任务。
失败了,滚动你自己的任务?
答案 3 :(得分:2)
我最近也不得不做类似的事情,并最终得到一个基于内联自定义任务的hacky但可行的解决方案。
<UsingTask TaskName="GetNewFileName" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<OriginalFile ParameterType="System.String" Required="true" />
<PackageVersion ParameterType="System.String" Required="true" />
<NewFile Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
<![CDATA[
int versionIndex = OriginalFile.IndexOf(PackageVersion);
versionIndex = versionIndex < 0 ? OriginalFile.Length : versionIndex;
NewFile = OriginalFile.Substring(0,versionIndex) + "nupkg";
]]>
</Code>
</Task>
</UsingTask>
<UsingTask TaskName="Combine" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Path ParameterType="System.String" Required="true" />
<File ParameterType="System.String" Required="true" />
<FullPath Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
<![CDATA[
FullPath = System.IO.Path.Combine(Path, File);
]]>
</Code>
</Task>
</UsingTask>
<Target Name="AfterCompile" Condition="'$(IsDesktopBuild)'!='true'">
<ItemGroup>
<Nuspecs Include="$(SolutionRoot)\$(WorkingBranch)\Nuspec\**\*.nuspec"/>
</ItemGroup>
<!-- Build nugets -->
<Message Text="DEBUG: Build nuget packages" />
<Exec
WorkingDirectory="$(NuspecsPath)"
Condition=""
Command='$(NugetPath) pack "%(Nuspecs.FullPath)" -Version 1.0.0.$(PackageVersion)' />
<ItemGroup>
<Nupkgs2 Include="$(NuspecsPath)\**\*.nupkg"/>
</ItemGroup>
<GetNewFileName OriginalFile="%(Nupkgs2.FileName)%(Nupkgs2.Extension)" PackageVersion="$(PackageVersion)">
<Output ItemName="RenamedNuPkgs" TaskParameter="NewFile"/>
</GetNewFileName>
<Combine File="%(RenamedNuPkgs.Filename)%(RenamedNuPkgs.Extension)" Path="$(NugetRepository)">
<Output ItemName="PackagesRepository" TaskParameter="FullPath"/>
</Combine>
<Message Text="Renamed nuget packages: @(RenamedNuPkgs)"/>
<Message Text="Repository Renamed nuget packages: @(PackagesRepository)"/>
<Copy SourceFiles="@(Nupkgs2)" DestinationFiles="@(PackagesRepository)"/>