Alex Rivera | Logout

How do I perform the EXEC task in a "loop" with MSBuild ItemGroups?

Asked 2011-05-17T20:32:25.813
55

How do I perform the EXEC task in a "loop" with MSBuild ItemGroups?

Instead of repeating this command over and over, like so:

    <Exec ContinueOnError="false" Command="sqlcmd -S $(ServerName) $(SqlServerUser) -d $(DbName) -v TableName=%22account%22 -i data\add_sql_cache.sql -b" />
    <Exec ContinueOnError="false" Command="sqlcmd -S $(ServerName) $(SqlServerUser) -d $(DbName) -v TableName=%22services%22 -i data\add_sql_cache.sql -b" />
    <Exec ContinueOnError="false" Command="sqlcmd -S $(ServerName) $(SqlServerUser) -d $(DbName) -v TableName=%22servicesGroup%22 -i data\add_sql_cache.sql -b" />
    <Exec ContinueOnError="false" Command="sqlcmd -S $(ServerName) $(SqlServerUser) -d $(DbName) -v TableName=%22servicesCategory%22 -i data\add_sql_cache.sql -b" />

I'd rather define an ItemGroup and just execute a "loop". I've got the ItemGroup down:

<ItemGroup>
    <CachedTables Include="account" />
    <CachedTables Include="services" />
    <CachedTables Include="servicesGroup" />
    <CachedTables Include="servicesCategory" />

But due to MSBuild's amazingly unintuitive syntax, I have no idea how to perform the Exec task in a loop with the ItemGroup above as an input.

Edit
Report

1 Answer

44

If I understand the question, it can be done a bit more simply. The following example uses <Exec/> to echo out each item in an <ItemGroup/>

  <ItemGroup>
    <MySpecialItem Include="one" />
    <MySpecialItem Include="two" />
    <MySpecialItem Include="three" />
  </ItemGroup>    

  <Target Name="MyTarget">
    <Exec Command="echo %(MySpecialItem.Identity)"/>
  </Target>
answered 2013-02-14T16:25:28.743

Your Answer