msbuild: How can I exclude wildcard paths matching

2019-08-12 22:53发布

问题:

I want to include all files which match *.c, including in subdirectories, except for any file where its path (beneath the solution root) contains one or more components (directory name or filename) beginning with an underscore.

For some reason, this isn't working:

 <ItemGroup>
    <ClCompile Condition="!$([System.Text.RegularExpressions.Regex]::IsMatch('%(Identity)', `.*[/\\]_.*`))" Include="../../src/foo/**/*.c"/>
</ItemGroup>

How can I do this in msbuild? I'm using msbuild 14.0.

回答1:

The sytnax you use is fine for filtering existing ItemGroups, but not while creating one (I don't immediately find where this is documented); which means this is ok:

<ItemGroup>
  <MatchingCFiles Include="../../src/foo/**/*.c"/>
  <ClCompile Condition="!$([System.Text.RegularExpressions.Regex]::IsMatch('%(Identity)', `.*[/\\]_.*`))" Include="@(MatchingCFiles)"/>
</ItemGroup>