tags:

views:

502

answers:

4

Hi,

I'm trying to build an ItemGroup in an MSBuild script which contains a list of folders directly below a given 'Root' folder. So - in this example...

+ Root folder
---- Sub Folder 1
-------- Sub-Sub Folder 1
-------- Sub-Sub Folder 2
---- Sub Folder 2
---- Sub Folder 3

... I would want my ItemGroup to contain "Sub Folder 1", "Sub Folder 2" and "Sub Folder 3".

There may be a number of files at any point in the hierarchy, but I'm only interested in the folders.

Can anyone help!?

Thanks, - Chris

+5  A: 
<PropertyGroup>
    <RootFolder>tmp</RootFolder>
</PropertyGroup>
<ItemGroup>
   <AllFiles Include="$(RootFolder)\**\*"/>
   <OnlyDirs Include="@(AllFiles->'%(Directory)')"/>
</ItemGroup>

@(OnlyDirs) might contain duplicates, so you could either use the RemoveDuplicatesTask :

<Target Name="foo">
   <RemoveDuplicates Input="@(OnlyDirs)">
      <Output TaskParameter="Filtered" ItemName="UniqueDirs"/>
   </RemoveDuplicates>
</Target>

or use CreateItem with batching for %(AllFiles.Identity) or with msbuild 3.5:

<Target Name="foo">
   <ItemGroup>
      <UniqueDirs Include="%(AllFiles.Directory)"/>
   </ItemGroup>
</Target>
Ankit
+1  A: 

Doesn't work if the folders are empty!

Chuck
This should really be a comment (you prob didnt have the ability to at the time). This answer by me drills into the problem you cite [which I also had]: http://stackoverflow.com/questions/1657684/creating-a-list-of-folders-in-an-itemgroup-using-msbuild/3307941#3307941
Ruben Bartelink
A: 

This MSDN Forum post has a custom task that deals with the empty directory case (upvoted accepted as its a v useful answer)

Ruben Bartelink
+2  A: 

The MSBuild Extension pack has a task called FindUnder, which returns an itemgroup of files or folders below a certain path. The following task will achieve what you want, returning an itemgroup containing Sub Folder 1, Sub Folder 2, and Sub Folder 3, but not Sub-Sub Folder 1 or Sub-Sub Folder 2:

<MSBuild.ExtensionPack.FileSystem.FindUnder
    TaskAction="FindDirectories"
    Path="$(RootFolder)"
    Recursive="False">
    <Output ItemName="FoundFolders" TaskParameter="FoundItems" />
</MSBuild.ExtensionPack.FileSystem.FindUnder>
David Keaveny