Skip to content

Instantly share code, notes, and snippets.

@robatwilliams
Last active March 23, 2022 00:44
Show Gist options
  • Save robatwilliams/6512233 to your computer and use it in GitHub Desktop.
Save robatwilliams/6512233 to your computer and use it in GitHub Desktop.
Get the names of directories under a specified directory in MSBuild
<PropertyGroup>
<ParentDir>$(MSBuildProjectDirectory)\some\folder</ParentDir><!-- This folder has subfolders folders a, b, c-->
</PropertyGroup>
<Target>
<ItemGroup>
<SubfolderFullPaths Include="$([System.IO.Directory]::GetDirectories('$(ParentDir)'))" />
<SubfolderNames Include="@(SubfolderFullPaths->'$([System.IO.Path]::GetFileName('%(SubfolderFullPaths.Identity)'))')" />
</ItemGroup>
<!-- SubfolderNames now has 3 items, with their Identity metadata "a", "b", "c" -->
<!-- If you want to use the well-known item metadata to define further metadata,
it seems you have to us the above as an intermediate step: -->
<SubfolderNames2 Include="$(SubfolderNames)" >
<!-- Identity refers to that of the Included items, so can't do this in SubfolderNames directly -->
<OutputDir>%(Identity)</OutputDir>
</SubfolderNames2>
</Target>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment