tags:

views:

33

answers:

3

In MSBuild I have a property which value is Name_Something. How can I get name part of this property.

A: 

It looks like you could use Item MetaData instead of a Property:

<ItemGroup>
    <Something Include="SomeValue">
        <Name>YourName</Name>
        <SecondName>Foo</SecondName>
    </Something>
</ItemGroup>
Filburt
A: 

If I understand your question correctly you are trying to get the substring of a MSBuild property. There is no direct way to do string manipulation in MSBuild, like in NAnt. So you have two options:

1). Create separate variables for each part and combine them:

<PropertyGroup>
  <Name>Name</Name>
  <Something>Something</Something>
  <Combined>$(Name)_$(Something)</Combined>
</PropertyGroup>

This works fine if the parts are known before hand, but not if you need to do this dynamically.

2). Write a customer MSBuild task that does the string manipulation. This would be your only option if it needed to done at runtime.

Todd
+3  A: 

With MSBuild 4

If you use MSBuild 4, you could use the new and shiny property function.

<PropertyGroup>
  <MyProperty>Name_Something</MyProperty>
</PropertyGroup>

<Target Name="SubString">
  <PropertyGroup>
    <PropertyName>$(MyProperty.Substring(0, $(MyProperty.IndexOf('_'))))</PropertyName>
  </PropertyGroup>

  <Message Text="PropertyName: $(PropertyName)"/>
</Target>

With MSBuild < 4

You could use the RegexReplace task of MSBuild Community Task

<PropertyGroup>
  <MyProperty>Name_Something</MyProperty>
</PropertyGroup>

<Target Name="RegexReplace">
  <RegexReplace Input="$(MyProperty)" Expression="_.*" Replacement="" Count="1">
    <Output ItemName ="PropertyNameRegex" TaskParameter="Output" />
  </RegexReplace>

  <Message Text="PropertyNameRegex: @(PropertyNameRegex)"/>
</Target>
madgnome
Both of your suggestions are correct. Too bad I couldn't apply second one because setting VSDBSQL SetVariable property dynamically doesn't work. I imagine substring would work with MSBuild 4 but I use MSBuild 3.
Sergej Andrejev