msbuildteamcitymsbuild-itemgroup

msbuild create itemgroup from property group


I want to pass a semi-colon separated list of strings.
Each string represents a file name.

    <PropertyGroup>
          <FileNames>Newtonsoft.Json;Reactive</FileNames>
          <PathToOutput>C:/</PathToOutput>
    </PropertyGroup>

Now I want to create an item group which should give me all the files in particular folder excluding list of Filename, something like:

<ItemGroup>
    <ReleaseFiles Include="$(PathToOutput)\**\*.*" Exclude="%(identity)-> identity.contains(%FileNames)"/>
</ItemGroup>

How do I iterate through current folder's files and match each ones name if it contains any one filename in Filenames variable.


Solution

  • Pretty sure this is a duplicate but I cannot find it at the moment so here it goes:

    In code:

    <PropertyGroup>
      <FileNames>Newtonsoft.Json;Reactive</FileNames>
      <PathToOutput>C:/</PathToOutput>
    </PropertyGroup>
    
    <Target Name="FilterBasedCommaSeperatedProperty">
      <ItemGroup>
        <!-- property -> item -->
        <Excl Include="$(FileNames)"/>
        <!-- list all and add metadata list -->
        <AllReleaseFiles Include="$(PathToOutput)\**\*.*">
          <Excl>%(Excl.Identity)</Excl>
        </AllReleaseFiles >
        <!-- filter to get list of files we don't want -->
        <FilesToExclude Include="@(AllReleaseFiles)"
                        Condition="$([System.String]::Copy('%(FileName)').Contains('%(Excl)'))"/>
        <!-- all but the ones to exclude --> 
        <ReleaseFiles Include="@(AllReleaseFiles)" Exclude="@(FilesToExclude)"/>
      </ItemGroup>
      <Message Text="%(ReleaseFiles.Identity)" />
    </Target>