如何根据文件名部分过滤MsBuild中的ItemGroup? [英] How to filter ItemGroup in MsBuild based on filename part?
问题描述
我有一个ItemGroup
,其中包含一些文件(而且我无法控制此列表的生成方式):
I have an ItemGroup
that contains some files (And I have no control on how this list is generated):
<ItemGroup>
<AllFiles Include="Assembly1.dll;Assembly1.Tests.dll"/>
<AllFiles Include="Assembly2.dll;Assembly2.Tests.dll"/>
...
</ItemGroup>
我想创建第二个ItemGroup
(基于第一个),仅用于匹配****.Tests.dll
的文件名.即FilteredFiles
应为:Assembly1.Tests.dll
,Assembly2.Tests.dll
,...
And I would like to create a second ItemGroup
(based on the first one) holding only for filenames matching ****.Tests.dll
. That is FilteredFiles
should be: Assembly1.Tests.dll
, Assembly2.Tests.dll
, ...
到目前为止,我已经尝试过:
So far I tried:
<ItemGroup>
<FilteredFiles Include="@(AllFiles)" Condition="$([System.Text.RegularExpressions.Regex]::IsMatch(%(Filename), '\.Tests\.dll'))"/>
</ItemGroup>
但是它似乎不起作用.
But it doesn't seem to work.
PS:我也想进行不区分大小写的匹配,但这是另一个问题.
PS: I would also like for non case sensitive matches but that's another issue.
推荐答案
您需要使用%而不是@使用项目批处理.这将逐项处理项目,而不是同时包含所有项目.您的条件正确,我认为您在其他地方找到了.
You need to use item batching using the % instead of the @. This will work on the items one by one instead of including them all at the same time. You had the condition right, which I assume you found somewhere else.
<ItemGroup>
<FilteredFiles Include="%(AllFiles.Identity)" Condition="$([System.Text.RegularExpressions.Regex]::IsMatch(%(Filename), '\.Tests\.dll'))"/>
</ItemGroup>
这篇关于如何根据文件名部分过滤MsBuild中的ItemGroup?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!