CitizenInsane
CitizenInsane

Reputation: 4855

How to filter ItemGroup in MsBuild based on filename part?

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>

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: I would also like for non case sensitive matches but that's another issue.

Upvotes: 9

Views: 4082

Answers (1)

Matt Slagle
Matt Slagle

Reputation: 1085

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>

Upvotes: 11

Related Questions