Alireza Hosseinitabar
Alireza Hosseinitabar

Reputation: 118

How to run a task in MSBuild, only if files count are more than zero?

I'm trying to run a command in MSBuild, only if the condition is true. What condition? I count the files of a specific extension and if there is no file, I want to run that task.

<Target Name="MakeSureProjectHasViewsAndPagesInIt" AfterTargets="PreBuildEvent">
    <Exec Command="echo PROJECT HAS NO CSHTML FILE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" Condition="dir *.cshtml /s" />
</Target>

I can't make the condition part work. I want to count all *.cshtml files in the current project's directory and subdirectories and if the result is zero, I want to run that command.

I'm stuck at how to write that condition. Can you help please?

Upvotes: 0

Views: 128

Answers (1)

Chris Conti
Chris Conti

Reputation: 11

You have to define a property with the count of the files

<PropertyGroup>
    <CSHTMLCount>$([System.IO.Directory]::GetFiles('$(MSBuildProjectDirectory)', '*.cshtml').Length)</CSHTMLCount>
</PropertyGroup>

<Target Name="MakeSureProjectHasViewsAndPagesInIt" Condition="'$(CSHTMLCount)' == '0'" AfterTargets="PreBuildEvent">
    <Exec Command="echo PROJECT HAS NO CSHTML FILE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" />
</Target>

Upvotes: 1

Related Questions