MSBUild:复制基于原始名称的文件(遵循模式) [英] MSBUild: Copy files with a name based on the original (following a pattern)
问题描述
我在文件夹中有一组文件。它们都有一个与模式DR __。*匹配的名称。我想将它们复制到另一个文件夹,但删除DR__前缀。我如何做到这一点与MSBuild?我以前这样使用NAnt:
I have a set of files inside a folder. They all have a name which matches the pattern DR__.*. I want to copy them to another folder, but removing the DR__ prefix. How can I do this with MSBuild? I used to do it like this using 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>
推荐答案
我同意@ Si的解决方案。但是使用MSBuild 4.0,你可以使用内置的功能。 NAnt脚本比我清楚得多。但我将它添加为解决方案只是为了展示MSBuild 4.0技术:
I agree with @Si's solution. But with MSBuild 4.0 you can do it with built in functionality. NAnt script is much clearer than mine. But I will add it as a solution just to show MSBuild 4.0 techniques:
<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)
复制中的$(DestinationFullPath)
源文件和目标文件。此外,我不得不更改前缀到 DR __
,因为 DR __。
将无法工作。
Edit (by OP): To get this working, I had to replace $(DestinationFullPath)
in Copy
with @(DestinationFullPath)
, to match the number of Source and Destination Files. Also, I had to change the prefix to DR__
, since DR__.
wouldn't work.
这篇关于MSBUild:复制基于原始名称的文件(遵循模式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!