Reputation: 46555
This should be simple, but I can't find how to do this (or maybe it's not possible).
In MSBuild I have an ItemGroup that is a list of files. I want to execute a task only if a particular file is in that ItemGroup
Something like:
<Copy Condition="@(Files) <contains> C:\MyFile.txt" .... />
Any way to do this? Preferably without writing a custom task.
Edit: The list of files is only to do with the condition. Otherwise it has no relation to the task.
Upvotes: 21
Views: 11229
Reputation: 111
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>
Upvotes: 0
Reputation: 4424
Try
<Copy Condition="'%(Files.Identity)' == 'C:\MyFile.txt'" .. />
Upvotes: 22