Loop - How to
use
Examples
Loop command is one of most powerful commands
of Automation Anywhere. There are 3 types of loop command.
Start Loop
Automation Anywhere provides 6 types of looping commands that you can use in your tasks.
- Loop For N times:
It will loop the actions between the Start Loop and End Loop commands for the number of
times specified by you. If you want to repeat some actions say 10 times, put these actions between Start and End Loop and specify 10.
- Loop For List variable:
You don't know how many time to loop, but let's say you have a list variable with
many values in it. Sometime they can be 10, sometime 100. Using this command you can repeat
the actions within the Start and end Loop block for as many times as there are
items in the list variable. You can access the list variable values within the loop.
For e.g. for the first time when the loop runs, you can get the first value of
the list variable, in the second time you can get the second value of the List variable and so on.
- Loop For Each file in a folder:
If you want to perform some actions for all files in a folder, use this
command. Often number of files in a folder can change. You can specify a
folder and this loop will run for as many times as there are number of files in
that folder. You can get the name of the file by using the System Variable $FileName$.
Similarly to get the extension of the file you can use $Extension$ System
variable and to get the path of the folder under which this file resides use the
System Variable $CurrentDirectory$. All these system variables will
return empty value when used out side this looping command.
- Loop For Each folder in a folder:
It is similar to Loop for each file in a
folder, except it loops for folders, not files. To access the folder name within the loop use
the System Variable $FolderName$.
- Loop For Condition:
You can loop for several
conditions. For e.g. you want to perform some set of actions over and over again
if a file exists. For doing that you can use this looping command.
- Loop For Each row in a SQL query dataset:
You have to use this looping command in conjunction with the
Database commands. Whenever you issue a SQL query using Automation Anywhere
Database SQL Query command, the result set of the SQL query is stored in the
System Variable $Dataset Column$.
Let's say you issued a SQL query to the database and you queried for 5 columns.
You can access the five columns of the result set in the query by calling
$Dataset Column(1)$, $Dataset Column(2)$, $Dataset Column(3)$, $Dataset
Column(4)$, $Dataset Column(5)$. This command is extremely powerful and helpful
in transferring data from your database to other applications like web page or Excel etc.
- Loop For Each row in an Internet Explorer Table:
You have to use this looping command in conjunction with
the Import Table option of Web Recorder command. Whenever you import
table using Automation Anywhere Web Recorder->Import Table command, the
rows and columns of the table are stored in the System Variable
$Table Column$. Let's say you imported table that
has 5 columns. You can access the five columns of the table by calling $Table
Column(1)$, $Table Column(2)$, $Table Column(3)$, $Table Column(4)$, $Table
Column(5)$. This command is extremely powerful and helpful in transferring
data from your web page table to other applications like web page or Excel
etc.
- Loop For Each Row in an Excel Dataset:
You have to use this
looping command in conjunction with the get Multiple Cells option of
Excel command. Whenever you have retrieved a range of cells from excel
using Excel-> Get Cells->Get Multiple Cells., the rows and columns of
the range are stored in the System Variable $Excel Column$. Let's say
you retrieved a range of cells from excel that have 5 columns. You
can access the five columns of the table by calling $Excel Column(1)$, $Excel
Column(2)$, $Excel Column(3)$, $Excel Column(4)$, $Excel Column(5)$. This
command is extremely powerful and helpful in transferring data from excel to
excel or any other application etc.

End Loop
Use this command to end the loop. The
commands that appear between the Start and End Loop will repeat based on the
type of the loop selected. End Loop command is always inserted every time you
insert a Start Loop. If it gets deleted then you can insert it again.
Exit Loop
Use this command to break from a loop if you do not want to execute the loop when certain conditions have
met. Exit Loop command can appear only between 'Start Loop' and 'End Loop' command.
Upon error continue with next loop (Available only in
Enterprise version)
You can use this option to continue with the next iteration in the loop if an
error occurs in any of the commands within the Loop-End Loop block. Optionally,
you can also log your custom text to a log file when an error occurs. For e.g.
if you are processing thousands of tracking ids and when one of the ids fails
then you don't want to stop the task and log the tracking id and continue with
the next one. In that case, you can use this option.