<foreach>

Loops over a set of items.

Can loop over files in directory, lines in a file, etc.

The property value is stored before the loop is done, and restored when the loop is finished.

The property is returned to its normal value once it is used. Read-only parameters cannot be overridden in this loop.

Parameters

Attribute Type Description Required
item
The type of iteration that should be done.
True
property string
The NAnt property name(s) that should be used for the current iterated item.
True
delim string
The deliminator char.
False
in string
The source of the iteration.
False
trim
The type of whitespace trimming that should be done. The default is NAnt.Core.Tasks.LoopTask.LoopTrim.None.
False
failonerror bool
Determines if task failure stops the build, or is just reported. The default is true.
False
if bool
If true then the task will be executed; otherwise, skipped. The default is true.
False
unless bool
Opposite of NAnt.Core.Task.IfDefined. If false then the task will be executed; otherwise, skipped. The default is false.
False
verbose bool
Determines whether the task should report detailed build log messages. The default is false.
False

Nested elements

<in>

Stuff to operate in. Just like the NAnt.Core.Tasks.LoopTask.Source attribute, but supports more complicated things like a NAnt.Core.Types.FileSet and such.

Please remove the NAnt.Core.Tasks.LoopTask.Source attribute if you are using this element.

</in>

<do>

Tasks to execute for each matching item.

</do>

Examples

Loops over the files in c:\.

    <foreach item="File" in="c:\" property="filename">
    <echo message="${filename}" />
</foreach>

Loops over all files in the project directory.

    <foreach item="File" property="filename">
    <in>
        <items>
            <include name="**" />
        </items>
    </in>
    <do>
        <echo message="${filename}" />
    </do>
</foreach>

Loops over the folders in c:\.

    <foreach item="Folder" in="c:\" property="foldername">
    <echo message="${foldername}" />
</foreach>

Loops over all folders in the project directory.

    <foreach item="Folder" property="foldername">
    <in>
        <items>
            <include name="**" />
        </items>
    </in>
    <do>
        <echo message="${foldername}" />
    </do>
</foreach>

Loops over a list.

    <foreach item="String" in="1 2,3" delim=" ," property="count">
    <echo message="${count}" />
</foreach>

Loops over lines in the file properties.csv, where each line is of the format name,value.

    <foreach item="Line" in="properties.csv" delim="," property="x,y">
    <echo message="Read pair ${x}=${y}" />
</foreach>

Requirements

Assembly
NAnt.Core.dll
Namespace
NAnt.Core.Tasks