This project is read-only.

Building ItemGroup with TextString StartsWith

Sep 28, 2009 at 7:36 PM

I would like to know how to filter an item using the MSBuild.ExtensionPack.Framework.TextString TaskAction="StartsWith" so that I get a subset of the original items.

For example if I have a list of items: "XXX_something; YYY_something; XXX_something_else", I might want to keep just the elements that start with "XXX_". I wish to create a new ItemGroup that contains just "XXX_something;XXX_something_else".

The code snippet belows - accomplishes part of what I want - it creates an item group called "TheResult" - but it is populated with true/false from the "StartsWith" test on the TextString task. I want to get the actual value (String1) that I am passing into StartsWith:




<Project ToolsVersion="3.5" DefaultTargets="Default" xmlns="">
    <Import Project="C:\Program Files\MSBuild\ExtensionPack\MSBuild.ExtensionPack.tasks"/>
    <AnItem Include = "NET">
    <AnItem Include = "NET">
    <AnItem Include = "NET">
    <Target Name="Default">
        <!-- Check whether a string starts with another string -->
        <MSBuild.ExtensionPack.Framework.TextString TaskAction="StartsWith" String1="%(AnItem.ASubItem)" String2="XXX">
            <Output ItemName="TheResult" TaskParameter="Result"/>
        <Message Text="The Result: @(TheResult)"/>

Sep 30, 2009 at 10:28 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Sep 30, 2009 at 11:07 PM

Hi, can you try the latest code. I've added a FilterItems TaskAction to the MSBuildHelper class.


From the sample:


<XXX Include="AA"/>
            <XXX Include="AAB"/>
            <XXX Include="ABA"/>
            <XXX Include="AABA"/>
            <XXX Include="BBAA"/>
            <YYY Include="AA">
            <YYY Include="AA">
            <YYY Include="BB">
            <YYY Include="CC">
            <YYY Include="DD">
        <!-- Filter Items based on Name -->
        <MSBuild.ExtensionPack.Framework.MsBuildHelper TaskAction="FilterItems" InputItems1="@(XXX)" RegexPattern="^AA">
            <Output TaskParameter="OutputItems" ItemName="filtered"/>
        <Message Text="filtered Items: %(filtered.Identity)"/>
        <!-- Filter Items based on MetaData -->
        <MSBuild.ExtensionPack.Framework.MsBuildHelper TaskAction="FilterItems" InputItems1="@(YYY)" Metadata="Filter" RegexPattern="^CC">
            <Output TaskParameter="OutputItems" ItemName="filteredbymeta"/>
        <Message Text="filteredbymeta Items: %(filteredbymeta.Identity)"/>

Oct 1, 2009 at 5:59 PM

Thanks. I was also able to do this with a custom task. E.g.:


    public class MyCustomTask : Microsoft.Build.Utilities.Task
        private ITaskItem[] _references;

        public ITaskItem[] References
            get { return _references;  }
            set { _references = value; }

        private ITaskItem[] _strippedReferences;

        public ITaskItem[] StrippedReferences
            get { return _strippedReferences; }
            set { _strippedReferences = value; }

        public override bool  Execute()
            Log.LogMessage(MessageImportance.Normal, "Yokely, Dokely");

            ArrayList items = new ArrayList();

            foreach (ITaskItem x in References)
                if (x.GetMetadata("ASubItem").StartsWith("XXX"))

            StrippedReferences = (ITaskItem[])items.ToArray(typeof(ITaskItem));

            return true;

Oct 1, 2009 at 8:09 PM

Ok, at a  quick glance i think the new FilterItems task will accomplish the same result.

Thanks for the feedback.