tags:

views:

262

answers:

2

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.

+2  A: 

How about using Exclude:

<CreateItem Include="@(Items)" Exclude="*test*" >
  <Output TaskParameter="Include" ItemName="ItemsToProcess"/>
</CreateItem>
KMoraz
I haven't tried it yet but it's so obvious I have pre-emptively smacked my forehead and said "doh!". Thanks.
justin.m.chase
+2  A: 

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>
Sayed Ibrahim Hashimi
Very close except that, unfortunately, I can't edit the <Items /> declaration (as specified in the original question). Sorry I wasn't more clear about that. But that would definitely work otherwise...
justin.m.chase
The solution was to basically do this except create a new set of items and use this new collection instead. It turns out that CreateItem doesn't respect Exclude and Condition. So I create a new ItemGroup using exclude like you have here then put a condition on CreateItem like the previous answer suggests. So it was a combination of the two... thanks everyone!
justin.m.chase