You use foreach to execute a sequence for each element in a list (see section “list”). foreach executes the child elements of each element in the selected list as a sequence.
You can specify whether the sequences are executed one after the other (in the same order as the list elements) or in parallel.
Format
|
Attributes
Name | Value | Meaning |
| string | Name of a valid context object of type list. |
| string | Name of the foreach context object which takes on the value of the current list element. This must not be defined in the foreach context. It is defined implicitly. The type of context object corresponds to the type set in the selectType attribute. |
|
| The default value is sequential. |
|
| Filters the elements of the specified type from the list. |
|
| The default value is forward. |
Examples
1. Distributing files
|
The files bin.mp3 and text.txt are copied to two computers.
In the example, the lists of files and computers are defined as context objects. The file list can also be defined, for example, by means of a listDirectory (see section “listDirectory”).
A double foreach sequence is used. The external sequence works through all the computers and the inner sequence works through all the files. The connection to the computers takes place in parallel and the files are also worked through in parallel at each computer.
autoDataSpec differentiates between text and binary files (see section “autoDataSpec”).
When the script is run, the files are distributed to all the computers.
Since no faulthandler was used in the example, the script is terminated with an error.
2. Copying the file tree
|
In the directory frg_eis_11 on the computer UnixP_1, the file tree *//* is copied to the directory frg_eis_11 under the local ID.
In the first foreach sequence, all the necessary directories are copied sequentially using createDirectory.
listDirectory returns the directories a, a/b and a/b/c in sequence for the directory frg_eis_11/a/b/c (frg_eis_11 is itself defined as the base directory with baseDir). A directory cannot be created unless the parent directory exists.
In the second foreach sequence, the files are copied in parallel since all the target directories are now present.
3. Deleting a file tree
|
In this example, everything in the directory frg_eis_10 on the computer UnixP_1 is deleted under the FTAC transfer admission FTACADM.
listDirectory (see section “listDirectory”) is used to determine all the files and directories recursively using the search pattern *//*. The sequence in which the directories are listed corresponds to the sequence required for their generation (i.e. the opposite sequence is required in order to delete them).
In the first foreach sequence, all the files are deleted in parallel. Non-existent files are ignored. An error during file deletion results in cancellation of the script.
In the second foreach sequence, the empty directories are deleted backwards because the directories to be deleted with deleteDirectory must be empty (see section “deleteDirectory”). Non-existent directories are ignored. Other errors result in the cancellation of the script.
When the script has run, the directory frg_eis_10 on the computer UnixP_1 is empty.