Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

foreach

&pagelevel(3)&pagelevel

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

 <foreach listRef="ID" contextObject="ID" execute?=
 "parallel|sequential"
          selectType="file|partner|directory" direction?=  
 "forward|reverse" >
   comment?
   context?
   Activity+
 </foreach

Attributes

Name

Value

Meaning

listRef

string

Name of a valid context object of type list.

contextObject

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.

execute?

parallel |
sequential

The default value is sequential.
The sequences are executed one after the other.
If parallel is specified then the sequences are started in parallel.

selectType

partner |
file |
directory

Filters the elements of the specified type from the list.
Only the filtered elements are iterated.

direction?

forward |
reverse

The default value is forward.
The list is worked through forwards.
If reverse is specified then the list is worked through backwards.

Examples

1. Distributing files

 <?xml version="1.0" encoding="UTF-8"?>
 <ftscript version="1">
   <context>
     <list id="FileList">
       <file name="bin.mp3"/>
       <file name="text.txt"/>
     </list>
     <list id="HostList">
       <partner name="UnixP_1" systemType="unix">
         <transferAdmission>
           <ftacAdmission ftacAdmission="FTACADM1"/>
         </transferAdmission>
       </partner>
       <partner name="WindowsP_1" systemType="windows">
         <transferAdmission>
           <ftacAdmission ftacAdmission="FTACADM2"/>
         </transferAdmission>
       </partner>
     </list>
   </context>
   <foreach listRef="HostList" selectType="partner"
            contextObject="partner" execute="parallel">
     <foreach listRef="FileList" selectType="file"
              contextObject="file" execute="parallel">
       <transferFile>
         <fromLocalFile ref="file">
           <directory name="W:/openFT/ftscript/Test/data/large"/>    
           <autoDataSpec binPattern="*.mp3" charPattern="*.txt"/>
         </fromLocalFile>
         <toRemoteFile ref="file">
           <partner ref="partner"/>
           <directory name="frg_eis_09"/>
         </toRemoteFile>
       </transferFile>
      </foreach>
   </foreach>
 </ftscript>

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

 <?xml version="1.0" encoding="UTF-8"?>
 <ftscript version="1">
   <context>
     <partner id="remote" name="UnixP_1">
       <transferAdmission>
         <ftacAdmission ftacAdmission="FTACADM1"/>
       </transferAdmission>
     </partner>
   </context>
   <listDirectory name="*//*" listObject="Flist">
     <partner ref="remote"/>
     <baseDir name="frg_eis_11"/>
   </listDirectory>
   <foreach listRef="Flist" selectType="directory"
            contextObject="creDir" execute="sequential">
     <createDirectory ref="creDir">
       <baseDir name="frg_eis_11"/>
     </createDirectory>
   </foreach>
   <foreach listRef="Flist" selectType="file"
            contextObject="file" execute="parallel">
     <transferFile>
       <fromRemoteFile ref="file">
         <partner ref="remote"/>
         <directory name="frg_eis_11"/>
       </fromRemoteFile>
       <toLocalFile ref="file">
         <directory name="frg_eis_11"/>
       </toLocalFile>
     </transferFile>
   </foreach>
 </ftscript>

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

 <?xml version="1.0" encoding="UTF-8"?>
 <ftscript version="1">
   <context>
     <partner id="remote" name="UnixP_1">
       <transferAdmission>
         <ftacAdmission ftacAdmission="FTACADM1"/>
       </transferAdmission>
     </partner>
   </context>
   <listDirectory name="frg_eis_10/*//*" listObject="Flist"> 

     <partner ref="remote"/>
   </listDirectory>
   <foreach listRef="Flist" selectType="file"
            contextObject="delFile" execute="parallel">
     <deleteFile ref="delFile">
       <partner ref="remote"/>
     </deleteFile>
   </foreach>
   <foreach listRef="Flist" selectType="directory"
            contextObject="delDir" execute="sequential"
                           direction="reverse">
     <deleteDirectory ref="delDir">
       <partner ref="remote"/>
     </deleteDirectory>
   </foreach>
 </ftscript>

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.