If-Else-Endif - Examples                         How to use?

 

Example 1: To close notepad window if open.

  1. Open the Task Editor from Tools -> Task Editor.
  2. Double click on the 'If' command.
  3. Select the option 'Window exists'.
  4. Make sure that 'Untitled - Notepad' is already open.
  5. Click on the 'Refresh' button.
  6. Select the title 'Untitled - Notepad' from the list.
  7. Click on 'Save' button.
  8. Insert the 'Close Window' command in between 'If - End if' statements.

Example 2: Search for a record, if found perform different actions

Often you want to perform a search for an item, a word or a record in your application. If found you want to perform some actions, and if not, perform some other actions.

  1. Search for a item, a word or a record that doesn't exist so that you see a window that says 'Not Found'.
  2. Open the Task Editor from Tools -> Task Editor.
  3. Double click on the 'If' command.
  4. Select the option 'Window exists'.
  5. Select the Not Found window title from the list.
  6. Click on 'Save' button.
  7. Insert the actions you want to perform between 'If - End if' statements.

Example 3: Make a copy of the log file, mylog.txt, if it exists

  1. Create a task from Tools->Task Editor.
  2. Double-click on If command and select 'File Exists' option.
  3. Specify the path of your mylog.txt file.
  4. Click on Save. It will insert If File Exists and End if command.
  5. Insert a Copy File command (from Files/Folders) between the IF-End if command.
  6. When you run the task, it will check if the file exists and if it does, it will make a copy of the file.

Example 4: Create a task that prompts user for value and if the user enters 'STOP', it stops the task.

  1. Insert a Prompt command that asks user for the input.
  2. Specify the option to assign the user input to a variable, say $User-Input$.
  3. Insert an IF condition that compares the value of $User-Input$ variable with the text 'Stop'. In the IF command, select the option 'Variable'. Click on 'Edit' button. Specify the variable $USer-Input$ in the first text box, select the operator 'Includes' and in the 'Fix' option write 'STOP'.
  4. Insert a Stop task command within the IF-End if commands.
  5. Set the Repeat property of the task to 'Repeat Until I Stop'.
  6. This task will keep on repeating until the user enters 'STOP' when prompted.

Example 4: To open the downloaded file if it exists else prompt for another file before proceeding.

  1. Open the Task Editor from Tools-> Task Editor
  2. Double click on 'If-Else-EndIf' command.
  3. Select 'If-File Exists' and enter the path of the file in the Select File e.g. C:\Documents and Settigns\...\Desktop\Downloadfile.txt
  4. Click 'Save' button.
  5. Double click on 'Open Program/File' command.
  6. Enter the program/file path.
  7. Click 'Save' button.
  8. Double click on 'If-Else-EndIf' command again.
  9. Select 'Else' and Click 'Save' button.
  10. Double click on 'Prompt for Filé/Folder'.
  11. Enter your custom caption and message.
  12. In the 'Select File/Folder', select 'File'.
  13. Assign it to a list varible from the list.
  14. Click 'Save' button.
  15. Double click on 'Open Program/File' command again.
  16. In the the program/file path, press F2 and insert the list varible which was used in 'Prompt for File/Folder'
  17. Click 'Save' button.

Tip: Using Wildcards (*)

  1. For the options 'Windows Exists', 'Window Does Not Exist' and 'Image Recognition' you can also specify wild cards i.e. *.
  2. For e.g. if you want to perform any operation if any window with the word 'Microsoft' in the title. It can have any string before or after the word 'Microsoft' but it is has this word.
  3. In that case you can specify IF Window Exists '*Microsoft*'.
  4. Automation Anywhere will first search for the exact window i.e. '*Microsoft*' and if it does not find it then it looks for windows that has the word 'Microsoft' in the title.
  5. You can put wild card character (*) at the beginning and at the end of the window title.