Display an Item List Separated with Commas
When you work with item lists in
MSBuild, it is sometimes useful to display the contents of those item lists in a way that is easy to read. Or, you might have a task that takes a list of items separated with a special separator string. In both of these cases, you can specify a separator string for an item list.
In this example, separators are specified for a list of items in a message and a list of search strings.
Default Separator
If you use the
Message element to specify the following message:
<Message Text="This is my list of TXT files: @(TXTFile)"/>
When the @(
*TXTFile*) item list contains the items App1.txt, App2.txt, and App3.txt, the message is:
This is my list of TXT files: App1.txt;App2.txt;App3.txt
By default,
MSBuild uses semicolons to separate items in a list.
Separator SyntaxIf you want to change the default, you can specify your own separator. The syntax for specifying an item list separator is:
@(ItemListName, '<separator>')
The separator can be either a single character or a string and must be enclosed in single quotes.
Comma SeparatorIn this example, the separator is a comma and a space:
<Message Text="This is my list of TXT files:
@(TXTFile, ', ')"/>
and the message is displayed as:
This is my list of TXT files: App1.txt, App2.txt, App3.txt
Project Files
Download CodeIn this example, the Exec task is used to run the tool findstr to find specified text strings in the file Phrases.txt. In the findstr command, literal search strings are indicated by the /c: parameter so the item separator " /c:" is inserted between items in the @(Phrase) list:
Command="findstr /i /c:@(Phrase, ' /c:') phrases.txt"/>
When the @(Phrase) item list contains hello, world, and msbuild, the equivalent command-line command is:
findstr /i /c:hello /c:world /c:msbuild phrases.txt
The complete project file is:
<Project
DefaultTargets = "Find"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<ItemGroup> <Phrase Include="hello"/>
<Phrase Include="world"/>
<Phrase Include="msbuild"/>
</ItemGroup> <Target Name = "Find">
<!-- Find some strings in a file -->
<Exec
Command="findstr /i /c:@(Phrase, ' /c:') phrases.txt"/>
</Target>
</Project>