I had a similar issue, where I wanted to do something if a file was not included in an itemGroup. In my case, it was the <Compile>
item group.
This snippet is scanning through all the files in the tag, and for each one, if the file's FullPath matches the TheFileIAmLookingFor
, then I set a property indicating the file was found.
Then I can use that property, and if it isn't set, I can assume the item wasn't included in the <Compile>
item group. In my case, the final action is to add the item only if it didn't exist. For context, I came across this issue while working on code-generation via custom msbuild build tasks, and the generated code was sometimes getting referenced twice, which was throwing a warning.
<Target Name="CheckForFile" BeforeTargets="CoreCompile"">
<PropertyGroup>
<DoesMyFileExist Condition="%(Compile.FullPath) == $(TheFileIAmLookingFor)">1</DoesMyFileExist>
</PropertyGroup>
<Message Importance="high" Condition="$(DoesMyFileExist)=='1'" Text="It exists"/>
<Message Importance="high" Condition="$(DoesMyFileExist)!='1'" Text="It does not exist!"/>
<ItemGroup Condition="$(DoesMyFileExist)!='1'">
<Compile Include="$(TheFileIAmLookingFor)" />
</ItemGroup>
</Target>