This came across the internal MSBuild discussion alias this week:

How can I concatenate a bunch of individual files into a single file during my build process? It looks like the ReadLinesFromFile and WriteLinesToFile tasks will do what I want, but I can't figure out what kind of batching operators to use.

Dan provided an elegant answer. The first step is to create an ItemGroup with the files you want to concatenate:

<ItemGroup>
   <InFiles Include="1.in;2.in"/>
</ItemGroup>

Then create a target to do the concatenation:

<Target Name="ConcatenateFiles">
   <ReadLinesFromFile File="%(InFiles.Identity)">
      <Output TaskParameter="Lines" ItemName="lines"/>
   </ReadLinesFromFile>
   <WriteLinesToFile File="test.out" Lines="@(Lines)" Overwrite="false" />
</Target>

The first task, ReadLinesFromFiles, goes through each file in the InFiles list and adds their lines to a new list called Lines. To ensure that each file is done in a loop (batched), the .Identity item metadata is tacked on. The lines that are read in are added to a new property called Lines, which is created by using the Output element. Then WriteLinesToFile takes the generated list and spits them out to a file.

Pretty nifty!

[ Author: Neil Enns ]