How to filter ItemGroup in MsBuild based on part of the file name?

I have an ItemGroup that contains some files (And I have no control over how this list is created):

 <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) containing only the file names matching ****.Tests.dll . This FilteredFiles should be: Assembly1.Tests.dll , Assembly2.Tests.dll , ...

So far I have tried:

 <ItemGroup> <FilteredFiles Include="@(AllFiles)" Condition="$([System.Text.RegularExpressions.Regex]::IsMatch(%(Filename), '\.Tests\.dll'))"/> </ItemGroup> 

But that does not work.

PS: I would also like if it weren’t for his coincidences, but this is another problem.

+6
source share
1 answer

You need to use a batch of elements using% instead of @. This will work on subjects one by one, and not include them all at the same time. You had the right condition, which I believe you found somewhere else.

 <ItemGroup> <FilteredFiles Include="%(AllFiles.Identity)" Condition="$([System.Text.RegularExpressions.Regex]::IsMatch(%(Filename), '\.Tests\.dll'))"/> </ItemGroup> 
+10
source

Source: https://habr.com/ru/post/956937/


All Articles