Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter ItemGroup in MsBuild based on filename part?

Tags:

msbuild

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.

like image 550
CitizenInsane Avatar asked Oct 29 '13 11:10

CitizenInsane


1 Answers

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>
like image 131
Matt Slagle Avatar answered Nov 20 '22 19:11

Matt Slagle