0

I want to create an ItemGroup of all bin directories within a given directory excluding all bin directories within given sub directories, e.g.:

MyDirectory\
  bin\
  ext\
    bin\
  lib\
    lib1\
      bin
  samples\
    sample1\
      bin\
  src\
    bin\

I want to delete all bin directories in MyDirectory, excluding all bin directories of ext and lib (i.e. delete samples\sample1\bin and src\bin).

This is what I came up with originally:

<ItemGroup>
  <ExcludedDirs Include="$(MyDirectory)\ext" />
  <ExcludedDirs Include="$(MyDirectory)\lib" />

  <BinDirs Include="$(MyDirectory)\**\bin" />
  <BinDirs Remove="$(MyDirectory)\%(ExcludedDirs.Identity)\**" />
</ItemGroup>

That doesn't work due to MSBuild - ItemGroup of all bin directories within subdirectories which is why I came up with the following:

<ItemGroup>
  <ExcludedDirs Include="$(MyDirectory)\ext" />
  <ExcludedDirs Include="$(MyDirectory)\lib" />

  <BinDirs Include="$([System.IO.Directory]::GetDirectories(&quot;$(SolutionDirectory)&quot;,&quot;bin&quot;, SearchOption.AllDirectories))" />
  <BinDirs Remove="$(MyDirectory)\%(ExcludedDirs.Identity)\**" />
</ItemGroup>

Now all bin diretoreis are in BinDirs, however, the bin directories in ext and lib are not excluded. What's the solution?

Community
  • 1
  • 1
D.R.
  • 20,268
  • 21
  • 102
  • 205

1 Answers1

2

Got the following insights:

  • ItemGroup's Remove is not able to work with wildcards on directories
  • There is an Exclude parameter, however, it does not support wildcards on directories as well

Therefore: Create a wildcard-free list of excluded directories first:

<ItemGroup>
  <_ExcludedDirs Include="ext" />
  <_ExcludedDirs Include="lib" />

  <ExcludedDirs Include="$([System.IO.Directory]::GetDirectories(&quot;$(MyDirectory)\%(_ExcludedDirs.Identity)&quot;,&quot;bin&quot;, SearchOption.AllDirectories))"/>
</ItemGroup>

Afterwards use it as the argument for ItemGroup's Exclude parameter:

<BinDirs Include="$([System.IO.Directory]::GetDirectories(&quot;$(SolutionDirectory)&quot;,&quot;bin&quot;, SearchOption.AllDirectories))" Exclude="@(ExcludedDirs)" />

Hope somebody can use this!

D.R.
  • 20,268
  • 21
  • 102
  • 205