53

I have this Msbuild code:

<Import Project="A.proj" Condition="$(BuildDefinition) =='Dist Staging to Dev' Or $(BuildDefinition) =='Dist Staging to Dev(Services Only)'"/>

But I was wondering if is there anything similar to check if an string contains some text to get something similar to:

<Import Project="A.proj" Condition="$(BuildDefinition) CONTAINS 'Dist Staging to Dev'"/>
Oscar Foley
  • 6,817
  • 8
  • 57
  • 90

3 Answers3

109

If you use MSBuild 4, you could use Property function

<Import Project="A.proj" 
        Condition="$(BuildDefinition.Contains('Dist Staging to Dev'))"/>

(More info on Property function)

David Gardiner
  • 16,892
  • 20
  • 80
  • 117
Julien Hoarau
  • 48,964
  • 20
  • 128
  • 117
  • Works, although the syntax seems somewhat counter-intuitive... in your example if the result of the condition were true then it might read condition="$(true)". If you used OR - condition="$(true) OR $(false)". So what does the $(...) do? Does it execute whatever's between the parenthesis or just write it out to the condition expression before execution? – Jacques Jan 07 '21 at 13:41
  • 1
    @Jacques, read the linked doc. `$` is a special character, that indicates what follows references a property. In this case, `BuildDefinition`. It won't be in the final string. The string will be `"true"`. In your second example, it would be `true OR false`, which presumably becomes `"true"` after evaluation. – ToolmakerSteve Nov 03 '22 at 20:51
  • Is there any way to make `Contains()` case insensitive? – l33t May 23 '23 at 11:06
19

MSBuild4: As Julien said, in MSBUILD 4 is possible to user Property Function.

MSBuild 3.x: In previous versions is possible if you use Tigris MsBuild Tasks

You can use task RegexMatch and use a regular expression

Oscar Foley
  • 6,817
  • 8
  • 57
  • 90
-5

MsBuild Conditions reference doesn't have anything about a possibility of a "contains" function. Looks like your first version is the only option.

Igor Zevaka
  • 74,528
  • 26
  • 112
  • 128
  • As others have pointed out, MSBuild 4 doesn't add new syntax for Conditions but lets you use property functions instead (http://blogs.msdn.com/b/msbuild/archive/2010/04/02/msbuild-property-functions.aspx). – BCran Apr 23 '13 at 10:01