Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a list of Folders in an ItemGroup using MSBuild

Tags:

msbuild

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!?

like image 452
Chris Roberts Avatar asked Nov 01 '09 17:11

Chris Roberts


2 Answers

In MSBuild 4.0 this is possible:

<ItemGroup>   <Folders Include="$([System.IO.Directory]::GetDirectories(&quot;$(RootFolder)&quot;))" /> </ItemGroup> 

Property Functions: http://msdn.microsoft.com/en-us/library/dd633440.aspx

like image 127
Jason Stangroome Avatar answered Sep 28 '22 08:09

Jason Stangroome


<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 Inputs="@(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> 
like image 38
radical Avatar answered Sep 28 '22 08:09

radical