For-Each Loops are used to iterate over a collection of items such as a list of files contained in aFile Listor a collection of data in aRowset. The For Each Loop will perform the same set of Tasks on each item in the collection.
Example Uses:
l Loop through a set of rows (records) retrieved from a database table (which is contained in a Rowset variable).
For each row in the loop, call a program to process the data.
l Loop through a set of files that were retrieved from a FTP server, which would be contained in a File List
variable. For each file in the loop, decrypt the file and send it to another internal server.
l Loop through a list of file attachments that were retrieved from an email server. For each file attachment in
the loop, encrypt the file and forward to another email address.
When looping through a Rowset (a collection of Rows), a row is exported to theCurrent Item Variablespecified on the For Each Loop. To access the value of a specific column in the row, use the following syntax in a task under the For Each Loop:
${rowVar[columnNumber]} or ${rowVar[columnName]}
When looping through a File List (a collection of files), the file information is exported to theCurrent Item Variable specified on the For Each Loop. For instance, to access the full path of the file, use the following syntax in a task under the For Each Loop:
${file}
To access specific file attributes, use the following syntax: ${file:size}
For a complete list of attributes, refer to theVariable Typesreference.
Basic Tab
Label
The label displayed in the Project Outline.
Items Variable
These are the items to iterate over. The value of this attribute must be a variable reference (for example, ${rowset} or ${filelist}).
Current Item Variable
The variable name which will be populated with the item at the current index. The variable can then be used in the tasks inside the loop or anywhere in the project (for example, "row" or "file").
Advanced Tab
ID
Identifiers are used to indicate which loop toExitorIterate.
Current Iteration Variable
The name of the variable that will contain a whole number that starts at 1 and is incremented by 1 every time the loop is executed.
Current Index Variable
The Current Index Variable is the variable name to which the current index of the iteration is exported. The value must be an identifier (for example, index). The value of this variable will be a whole number that represents the current index of the loop and the value is updated every time the loop advances. For example, if the loop is defined with begin=10, end=100 and step=10, then the code inside the loop will execute 10 times. The first time, the value of this variable will be 10, then the next iteration is 20, and the last iteration is 100.
Begin Index
The value must be a whole number and should be greater than zero. It must also be less than or equal to the End Index. If not specified, this value defaults to 1, which means processing starts with the first item in the Items Variable.
End Index
The ending index of the loop. The value must be a positive whole number, greater than or equal to the Begin Index.
Step
The Step is how the current index of the loop should be incremented. The value can be any positive whole number. The loop will increment by this number from the Begin Index until the End Index is met.
Control Tab
Log Level
Specify the level of log messages that will be generated when this Loop executes. If not specified, then the log level will be inherited from the Module or Loop it is contained within.
All tasks within this Loop will inherit the specified log level from the Loop, unless the log level is specifically overridden on the individual tasks.
Silent Only logs any errors that are encountered in the Loop.
Normal Additionally logs the start and stop times of the Loop, as well as the times of any Loops and Tasks contained in the Loop.
Verbose Additionally logs task-level details, such as the names of the files that were processed. Debug Additionally logs detailed debugging information, such as message responses from servers.
For-Each Loop Example
A particular FTP Server may not accept multiple files at a time from a client. In order to send multiple files to that FTP Server, you could instead use a For Each Loop on the File List to perform a Manual Put one file at a time. In this scenario, only files ending with "txt" needs to be sent to the FTP server.
1. Create a File list of files to send to the FTP Server:
a. From within the Project Designer screen, click the Actionlink in the sub-menu bar and click theAdd a Taskoption.
b. On theAdd Taskscreen, open theLocal File System Tasksfolder and click theCreate File List task.
c. On theCreate File Listscreen, specify theFile List Variable.
2. Specify the File Set that contains the files to transfer:
a. Click the Actionlink in the sub-menu bar and select theFile Setmenu item.
b. In theFile Setscreen, type the complete directory path to theBase Directorythat contains the files or click the icon to browse for the directory.
3. Create a Wildcard filter to find files ending with "txt" and include them in the fileset:
a. Click the Actionlink in the sub-menu bar and select theAdd Wildcard Filtermenu item. b. On theWildcard Filterscreen, click the Actionlink in the sub-menu bar and then select the
Include Filesmenu item.
c. In theWildcard Patternscreen, type the filter pattern.
4. Create the For Each Loop:
a. On theProject Outlinepane, click the module name to return to the Module level. b. From the Actionlink in the sub-menu select the Add For-Each Loopmenu item. c. Specify theItems Variableand theCurrent Item Variablefor the For Each Loop.
5. Create the FTP Task to put the files on the FTP Server:
a. On theFor-Each Loopscreen, click the Actionlink in the sub-menu and select theAdd a Task... menu item.
b. On theAdd Taskscreen, open theFTPfolder and click theFTPtask. c. Specify theFTP Server.
6. Under the FTP Task, create the Manual Put element to put each file individually on the server without additional file path information:
a. On theFTP Taskscreen, click the Actionlink in the sub-menu and select thePut a File Manually menu item.
b. Specify theSource Filevariable and theDestination Filevariable.