msbuild.netmsbuild-4.0msbuild-propertygroup

how to set property value based on item and use that property as input of property function?


I am trying to set a property value based on Item But property doesn't seem to notice Item For example, my code is like

<ItemGroup>
   <temp Include="test.txt"></temp>
</ItemGroup>

<PropertyGroup>
  <Name>%(temp.Identity)</Name>
  <Test>$([system.IO.File]::ReadAllText('$(Name)').Replace('ABC', '###'))</Test> 
</PropertyGroup>    

<Target Name="Write">
   <Message Text="$(Name)"/>
   <WriteLinesToFile File="%(temp.Filename).asl" Lines="$(Test)" Overwrite="true"/> 
</Target>

But when I run that, it says it cannot read %(temp.Identity) Am I missing anything?? Thank you!


Solution

  • Properties are evaluated before any items. Move the group into a target for delayed evaluation, your "Write" or a generic InitialTargets one.

    See Property and Item Evaluation Order.