Thierry Lelegard
Thierry Lelegard

Reputation: 691

Using MSBuild, how to expand wildcard files as one single line

Using MSBuild, after building an exe, I want to run it with a list of files coming from a wildcard expression.

I currently use something like this:

  <Target Name="AfterBuild">
    <ItemGroup>
      <SigFiles Include="$(RootDir)tables\*.xml"/>
    </ItemGroup>
    <Exec Command="$(OutDir)app.exe --merge %(SigFiles.FullPath) -o $(OutDir)model.xml"/>
  </Target>

The problem is that the command is executed once per file, using one file per command. With 3 .xml files, for instance, the following commands are run (paths omitted):

app.exe --merge a.xml -o model.xml
app.exe --merge b.xml -o model.xml
app.exe --merge c.xml -o model.xml

While I would like to run one single command with all files, blank separated:

app.exe --merge a.xml b.xml c.xml -o model.xml

How could I achieve this? No need to mention that I am not an MSBuild expert. Thanks

Upvotes: 0

Views: 118

Answers (1)

Jonathan Dodds
Jonathan Dodds

Reputation: 5008

When the '%' is used as you have it, a feature of MSBuild known as 'batching' is invoked. As you noted, the task is run multiple times, once for each item of the batch.

To reference the whole collection use '@'. The default delimiter between items is the semi-colon (;).

As an example, the following will display something like

a.xml;b.xml;c.xml

    <Message Text="@(SigFiles)" />

A different delimiter can be specified. The following example uses a space.

    <Message Text="@(SigFiles, ' ')" />

An item can also be 'transformed'. Your code is using FullPath. The following is not batching. The '%' is part of the transform expression.

    <Message Text="@(SigFiles->'%(FullPath)')" />

Putting the transform and the delimiter together:

    <Message Text="@(SigFiles->'%(FullPath)', ' ')" />

Taking the syntax we have built up and applying it to your original code:

    <Exec Command="$(OutDir)app.exe --merge @(SigFiles->'%(FullPath)', ' ') -o $(OutDir)model.xml"/>

Upvotes: 1

Related Questions