Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if directory is empty using MSBuild

Tags:

msbuild

Is there a convenient way to check if a directory is empty using MSBuild?

like image 446
Steve Avatar asked Mar 24 '11 14:03

Steve


2 Answers

Create an item and see if it contains anything, like this:

<Target Name="CheckDirectoryForEmpty">
  <PropertyGroup>
    <EmptyCheck>./PathTo/DirectoryToCheck/*.*</EmptyCheck>
  </PropertyGroup>
  <ItemGroup>
    <EmptyCheck Include="$(EmptyCheck)" />
  </ItemGroup>
  <Message
    Condition="'@(EmptyCheck)' == ''"
    Text="Directory '$(EmptyCheck)' is empty"
    />
</Target>

To check recursively, use **/*.* instead of *.* in the path.

like image 100
Brian Kretzler Avatar answered Nov 09 '22 11:11

Brian Kretzler


You could use one of the tasks in the MSBuild Extension Pack for this. Use the FindUnder task, and check whether FoundItems is empty.

like image 2
Andrew Brown Avatar answered Nov 09 '22 11:11

Andrew Brown