Subscribe

UiPath StudioX

The UiPath StudioX Guide

Tutorial: Working with Files and Folders

In this tutorial, we will create an automation that saves the attachments to all the emails in an Outlook Search Folder to a folder that contains today's date in the name and creates a text file where all the file names are copied. The automation will first check if the folder where to save the files already exists and, if that is the case, it will ask the user if the folder should be deleted and recreated or if the contents should be moved to a different folder before the attachments are downloaded.

We will start by adding a File Exists activity to check if the folder where we want to download the attachments already exists. We will then add an If activity and inside it we will add a Create Folder activity to create the folder if it does not exist. If the folder does exist, we will add a Message Box activity to prompt the user to select how to proceed. Another If activity will follow with two separate sets of activities to execute depending on the user's choice - either delete the folder and recreate it or move the existing folder to another location. We will then add the activities to save the attachments from Outlook, and then we will create two folders inside the destination folder, one in which to create the text file with information about the saved files and one in which to move the files after we add the file info to the text file. To add today's date to the folder name, we will use a formula built-in to the Excel Workspace.

Step 1: Create a Search Folder in Outlook.

  1. Open Outlook, then right-click Search Folders in the Folder pane and select New Search Folder.
  2. In the New Search folder window, select Create a custom Search Folder > Choose.
  3. In the Custom Search Folder window, enter "Last week's attachments" in the Name box and then select Criteria.
  4. In the Messages tab of the Search Folder Criteria window, from the drop-down menus next to Time select received and Last Week.
  5. From the More Choices tab, select Only items with, and then one or more attachments from the adjacent drop-down menu.
  6. Click OK three times to close the windows and save the search folder.

Step 2: Set up the project.

Step 3: Check if the folder where to save the attachments exists.

  1. In the Activities panel, select the System tab, and then double-click Folder Exists. A Folder Exists activity is added to the Designer panel.
  2. In the Folder Exists activity:
    • Click plus button on the right side of the Folder path field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the plus button on the right side of the Text Builder, select Workspace: Excel > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Excel] Date!YYYYMMDD. Click Save in the Text Builder.
    • Click plus button on the right side of the Output field, and then select Save for Later Use. Give the value the name Folder 'MailAttachments' Exists and click OK
      You have indicated that you want to check if a folder MailAttachments with today's date exists and that you want the output result of this verification to be saved for later use in your automation.

Step 4: Add activities to execute when the folder exists and when the folder does not exist.

  1. In the Activities panel, select the Common tab, and then drag an If activity and drop it below the Folder Exists activity. An If activity is added to the Designer panel.
  2. In the If activity, click plus button on the right side of the Condition field, and then select Condition Builder.
  3. In the Condition Builder, add the condition "if the folder exists":
    • Click plus button on the right side of the field on the left, and then select Use Saved Value > Folder 'MailAttachments' Exists to select the output of the Folder Exists activity as the value to evaluate in the condition.
    • From the drop-down menu in the middle, select is true, and then click Save.
      The activities you add in the Then branch of the If activity will be executed if the folder exists and the ones added to the Else branch will be executed if the folder does not exist.
  4. In the Activities panel, select the System tab, and then drag a Create Folder activity and drop it inside the Else branch of the If activity. A Create Folder activity is added to the Designer panel.
  5. In the Create Folder activity, click plus button on the right side of the Folder name field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the plus button on the right side of the Text Builder, select Workspace: Excel > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Excel] Date!YYYYMMDD. Click Save in the Text Builder.
    You have indicated that you want to create a folder MailAttachments with today's date in the name if the folder does not already exist.
  6. In the Activities panel, drag a Message Box activity and drop it inside the Then branch of the If activity. A Message Box is added to the Designer panel.
  7. Click plus button on the right side of the Message Box activity, and then select Text. In the Text Builder, enter a message to display to the user, for example:
    A 'MailAttachments_ folder already exists. Do you want to delete the old one and create a new one? If you choose "No", the old one will be moved to a backup folder and a new one will be created.. To add the current date to the folder name, place the mouse cursor after MailAttachments_, and then, from the plus button on the right side of the Text Builder, select Workspace: Excel > Date [Sheet] > YYYYMMDD [Cell]. Click Save in the Text Builder.
  8. Select the Message Box activity you added to the Designer panel, and then, in the Properties panel
    • In the DisplayName text box, enter Message Box 'FolderAlreadyExists'. This is the name displayed for the Message Box in the Designer panel.
    • From the Buttons drop-down menu, select YesNo.
      You have indicated that you want to prompt the user to choose if the folder should be removed if it already exists by displaying a Message Box with two button options Yes and No.
909

Step 5: Add activities to execute when the user selects to delete any existing folder with the same name or move it.

  1. In the Activities panel, select the Common tab, and then drag another If activity and drop it inside the Then branch of the first If activity below the Message Box . An If activity is added to the Designer panel.
  2. In the If activity, click plus button on the right side of the Condition field, and then select Condition Builder.
  3. In the Condition Builder, add the condition "if the user clicked Yes":
    • Click plus button on the right side of the field on the left, and then select Use Saved Value > ChosenButton from Message Box FolderAlreadyExists. This selects the automatically generated output of the Message Box activity as the value to evaluate in the condition.
    • From the drop-down menu in the middle, select equal to.
    • Click plus button on the right side of the field on the right, and then select Text. In the Text Builder, enter Yes, and then click Save twice.
      The activities you add in the Then branch of the If activity will be executed if the user clicked Yes and the ones in the Else branch will be executed if the user clicked No.
  4. In the Activities panel, select the System tab, and then drag a Delete Folder activity and drop it inside the Then branch of the second If activity. A Delete Folder activity is added to the Designer panel.
  5. In the Delete Folder activity, click plus button on the right side of the Folder name field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the plus button on the right side of the Text Builder, select Workspace: Excel > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Excel] Date!YYYYMMDD. Click Save in the Text Builder.
    You have indicated that you want to delete the existing folder MailAttachments with today's date in the name.
  6. In the Activities panel, drag a Create Folder activity and drop it inside the Then branch of the second If activity below Delete Folder. A Create Folder activity is added to the Designer panel.
  7. In the Create Folder activity, click plus button on the right side of the Folder name field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the plus button on the right side of the Text Builder, select Workspace: Excel > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Excel] Date!YYYYMMDD. Click Save in the Text Builder.
    You have indicated that you want to create a new folder MailAttachments with today's date in the name.
  8. In the Activities panel, drag a Create Folder activity and drop it inside the Else branch of the second If activity. A Create Folder activity is added to the Designer panel.
  9. In the Create Folder activity, click plus button on the right side of the Folder name field, and then select Text. In the Text Builder, enter C:\old_MailAttachments, and then click Save.
    You have indicated that you want to create a new folder called old_MailAttachments.
  10. In the Activities panel, drag a Move Folder activity and drop it inside the Else branch of the second If activity below Create Folder. A Move Folder activity is added to the Designer panel.
  11. In the Move Folder activity:
  • Click plus button on the right side of the From field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the plus button on the right side of the Text Builder, select Workspace: Excel > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Excel] Date!YYYYMMDD. Click Save in the Text Builder.
  • Click plus button on the right side of the To field, and then select Text. In the Text Builder, enter C:\old_MailAttachments, and then click Save.
    You have indicated that you want to move the existing folder MailAttachments with today's date in the name to the folder old_MailAttachments.
1080

Step 6: Save the attachments from the Outlook Search Folder.

  1. In the Activities panel, select the Outlook tab, and then drag an Outlook Account Card and drop it below the first If activity as the last activity in the project. An Outlook Account Card activity is added to the Designer panel.
  2. In the activity, the default email account is already selected in the Select mail account field. If you want to use a different account, select it from the drop-down menu.
    In the And reference it later as field, leave the default value Outlook as the name under which to refer to the account in the automation.
  3. In the Activities panel, drag the For Each Mail Message activity and drop it inside the Outlook Account Card.
  4. In the For Each Mail Message activity, click plus button on the right side of the Use mails from field, and then select Outlook > [Search Folders] > [Last week's attachments] to select the search folder created in the account Outlook. Your selection is displayed in the Use mails from field as "Last week's attachments".
    You have indicated that you want to iterate through the emails in the Search Folder Last week's attachments.
  5. In the Activities panel, drag the Save Mail Attachments activity and drop it inside the For Each Mail Message activity.
  6. In the Save Mail Attachments activity:
    • Click plus button on the right side of the From message field, and then select Current Mail to indicate that the email whose attachments you want to save is the current email you are iterating through.
    • Click plus button on the right side of the Save to folder field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the plus button on the right side of the Text Builder, select Workspace: Excel > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Excel] Date!YYYYMMDD. Click Save in the Text Builder.
      You have indicated that you want to save the attachments to the folder MailAttachments with today's date in the name.
578

Step 7: Create the text file where to add info about the files, create subfolders where to save the text file and move the attachments.

  1. In the Activities panel, select the System tab, and then drag a Create Folder activity and drop it below the Outlook Account Card as the last activity in the project. A Create Folder activity is added to the Designer panel.
  2. In the Create Folder activity, click plus button on the right side of the Folder name field, and then select Text. In the Text Builder, enter C:\MailAttachments_, then, from the plus button on the right side of the Text Builder, select Workspace: Excel > Date [Sheet] > YYYYMMDD [Cell], and then enter \Output. The text should look like this: C:\MailAttachments_[Excel] Date!YYYYMMDD\Output. Click Save in the Text Builder.
    You have indicated that you want to create a subfolder named Output in the MailAttachments folder.
  3. In the Activities panel, drag a Create File activity and drop it below the last Create Folder.
  4. Select the Create File activity you added to the Designer panel, and then, in the Properties panel, click plus button on the right side of the Path field, and then select Text. In the Text Builder, enter C:\MailAttachments_, then, from the plus button on the right side of the Text Builder, select Workspace: Excel > Date [Sheet] > YYYYMMDD [Cell], and then enter \Output\AttachmentsDetails.txt. The text should look like this: C:\MailAttachments_[Excel] Date!YYYYMMDD\Output\AttachmentsDetails.txt. Click Save in the Text Builder.
    You have indicated that you want to create a file called AttachmentsDetails.txt in the Output subfolder.
  5. In the Activities panel, drag a Write Text File activity and drop it below the Create File activity. A Write Text File activity is added to the Designer panel.
  6. In the Write Text File activity:
    • Click plus button on the right side of the Text field, and then select Text. In the Text Builder, enter Files received in the last week:. Click Save in the Text Builder.
    • Click plus button on the right side of the Write to filename field, and then select Text. In the Text Builder, enter C:\MailAttachments_, then, from the plus button on the right side of the Text Builder, select Workspace: Excel > Date [Sheet] > YYYYMMDD [Cell], and then enter \Output\AttachmentsDetails.txt. The text should look like this: C:\MailAttachments_[Excel] Date!YYYYMMDD\Output\AttachmentsDetails.txt. Click Save in the Text Builder.
      You have indicated that you want to enter the text "Files received in the last week:" in the file AttachmentsDetails.txt.
  7. In the Activities panel, drag a Create Folder activity and drop it below the Write Text File activity. A Create Folder activity is added to the Designer panel.
  8. In the Create Folder activity, click plus button on the right side of the Folder name field, and then select Text. In the Text Builder, enter C:\MailAttachments_, then, from the plus button on the right side of the Text Builder, select Workspace: Excel > Date [Sheet] > YYYYMMDD [Cell], and then enter \Processed. The text should look like this: C:\MailAttachments_[Excel] Date!YYYYMMDD\Processed. Click Save in the Text Builder.
    You have indicated that you want to create a subfolder named Processed in the MailAttachments folder.
435

Step 8: Iterate through the files, add info about each file to the text file, and move each file to the Processed subfolder.

  1. In the Activities panel, drag a For Each File in Folder activity and drop it below the last Create Folder activity. A For Each File in Folder activity is added to the Designer panel.
  2. In the For Each File in Folder activity, click plus button on the right side of the Folder field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the plus button on the right side of the Text Builder, select Workspace: Excel > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Excel] Date!YYYYMMDD\Output. Click Save in the Text Builder.
    You have indicated that you want to iterate through the files in the MailAttachments folder.
  3. In the Activities panel, drag an Append Line activity and drop it inside the For Each File in Folder activity. An Append Line activity is added to the Designer panel.
  4. In the Append Line activity:
    • Click plus button on the right side of the Text field, and then select Text. In the Text Builder, enter FileName:, then, from the plus button on the right side of the Text Builder, select Current File > Name, then enter | LastModifiedDate:, then, from the plus button on the right side of the Text Builder, select Current File > Last Modified Date . The text should look like this: FileName: [CurrentFile] Name | LastModifiedDate: [CurrentFile] LastModifiedDate. Click Save in the Text Builder.
    • Click plus button on the right side of the Write to filename field, and then select Text. In the Text Builder, enter C:\MailAttachments_, then, from the plus button on the right side of the Text Builder, select Workspace: Excel > Date [Sheet] > YYYYMMDD [Cell], and then enter \Output\AttachmentsDetails.txt. The text should look like this: C:\MailAttachments_[Excel] Date!YYYYMMDD\Output\AttachmentsDetails.txt. Click Save in the Text Builder.
      You have indicated that you want to enter the file name and the time the file was modified in the file AttachmentsDetails.txt for the current file that you are iterating through.
  5. In the Activities panel, drag a Move File activity and drop it inside the For Each File in Folder activity below Append Line. A Move File activity is added to the Designer panel.
  6. In the Move File activity:
    • Click plus button on the right side of the From field, and then select Current File > Full Path.
    • Click plus button on the right side of the To field, and then select Text, In the Text Builder, enter C:\MailAttachments_, then, from the plus button on the right side of the Text Builder, select Workspace: Excel > Date [Sheet] > YYYYMMDD [Cell], and then enter \Processed. The text should look like this: C:\MailAttachments_[Excel] Date!YYYYMMDD\Processed. Click Save in the Text Builder.
      You have indicated that you want to move the current file that you are iterating through to the Processed subfolder.
537
  1. Click Save in the ribbon menu to save the automation, then click Run to execute the automation.

The attachments in the Outlook Search Folder are saved to the MailAttachments folder, a text file with information about the files is created in the Output subfolder, and then the files are moved to the Processed subfolder. If you run the project again, because the folder was already created during the first run, you are prompted to select what to do with the existing folder before the other activities are executed.

Download example

Updated 2 years ago


Tutorial: Working with Files and Folders


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.