Automation Engine Script Guide > Introduction > First Steps > Loops

Loops in Scripts

Objectives:
- WHILE loops
- PROCESS loops

Lesson 8

Loops process particular scripting lines several times. AE Script provides two different loops that can be used:

Loops repeat a particular block until a particular condition is met. The iteration ends when the condition does not apply any more.

Note that script processing will abort if a WHILE loop is iterated many times in succession. This behavior serves to avoid infinite loops.

A reference to a data sequence is passed on to the process loop. The number of loop cycles results from the data sequence's number of lines. A data sequence is an internal list (such as a list of files or the entries of Variable objects). The values that the data sequence includes depend on the particular script element.


The following example describes how to use WHILE loops.

The script checks whether a particular agent is available. If so, a job should start on this agent. The script can be located in any executable object (such as SCRI). The only requirement is an agent on which the user is allowed to execute jobs.

In the first step, use the script element SYS_HOST_ALIVE in order to verify that the agent is active. The following example uses the agent WIN03. The agent is available if the script element returns "Y".

:SET &AGENT# = WIN03
:SET &ALIVE# = SYS_HOST_ALIVE(&AGENT#)

The next step is to create a WHILE loop. It should repeatedly check the agent's availability with a particular delay (the intention is to avoid too many loop cycles).
The loop should end when the agent is active. Therefore, the loop condition must be: &ALIVE# NE "Y". The comparison operator "NE" corresponds to "Does not equal". Other comparison operators are described in the documentation of the script element :WHILE.

:WHILE &ALIVE# NE "Y"
: PRINT "Waiting for agent &AGENT#..."
: WAIT 60
: SET &ALIVE# = SYS_HOST_ALIVE(&AGENT#)
:ENDWHILE

:SET &ACT# = ACTIVATE_UC_OBJECT(TESTJOB
)

If the agent is not active, the loop instructions are repeated. The script statement :WAIT delays script processing for a while (in this case, the waiting time is 60 seconds).

If the agent is available again, the verification (SYS_HOST_ALIVE) returns the value "Y". The loop ends and the scripting line that activates the job "TESTJOB" (script function ACTIVATE_UC_OBJECT) is read. The activation protocol indicates whether the system is still waiting for the agent or whether the job has already been activated.


The following steps describe the handling of process loops:

The objective is to obtain a file list from a computer and output it in the activation protocol.

The script element PREP_PROCESS_FILENAME is used to obtain the file list from an agent's computer. Filter keys serve to select files with particular names. The file list is provided as a data sequence in the script.

A process loop is required in order to process this data sequence line by line. It starts with :PROCESS and ends with :ENDPROCESS. For each line in the data sequence, the loop is processed once.

The script element GET_PROCESS_LINE retrieves the content of the current data sequence line. In this example, the result is the path and name of the file. It is output in the activation report.

The following example uses the agent WIN01 in order to retrieve the file list. We retrieve all text files of the directory "C:\AUTOMIC\Agents\WIN01\temp" which includes the agent's log files.

:SET&HND# = PREP_PROCESS_FILENAME("WIN01", "C:\AUTOMIC\Agents\WIN01\temp\*.txt","Y",,)
:
PROCESS&HND#
:   
SET&LINE#=GET_PROCESS_LINE(&HND#)
:  
PRINT&LINE#
:
ENDPROCESS

The result is that the paths and names of all selected files are output in the activation protocol.