justin.m.chase
justin.m.chase

Reputation: 13685

Select Files containing a certain substring in MSBuild

Suppose I have a bunch of files in a folder:

foo.test.txt
bar.txt
...

And I want to create an ItemGroup to exclude files containing ".test." somewhere in the title, how would I do that in MSBuild?

<!-- Can't change this part -->
<Items Include="*.txt" />

<CreateItem Include="@(Items)" Condition="'%(Items.Exclude)'!='true' AND (???)">
  <Output TaskParameter="Include" ItemName="ItemsToProcess"/>
</CreateItem>

Where the ??? should be something like:

!Contains(%(Items), ".test.")

Except that I don't know how to do that in MSBuild.

Upvotes: 2

Views: 1155

Answers (2)

Sayed Ibrahim Hashimi
Sayed Ibrahim Hashimi

Reputation: 44342

KMoraz is off to a good start, but since MSBuild 3.5 you can just use the ItemGroup syntax even inside of targets. So that would be something like:

<Project ...>
    <ItemGroup>
        <Items Include="*" Exclude="*.text.*"/>
    </ItemGroup>
</Project>

Upvotes: 2

KMoraz
KMoraz

Reputation: 14164

How about using Exclude:

<CreateItem Include="@(Items)" Exclude="*test*" >
  <Output TaskParameter="Include" ItemName="ItemsToProcess"/>
</CreateItem>

Upvotes: 3

Related Questions