studiox
2022.10
false
UiPath logo, featuring letters U and I in white
StudioX User Guide
Last updated Nov 18, 2024

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 Project Notebook.

  1. 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.
  2. Step 2: Set up the project.
  3. Step 3: Check if the folder where to save the attachments exists.
    1. In the Activities panel, select the File 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 docs image 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 docs image on the right side of the Text Builder, select Project Notebook (Notes) > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Excel] Date!YYYYMMDD. Click Save in the Text Builder.
      • Click Plus docs image 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.

  4. 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 Show Else to add the Else branch.
      • Click Plus docs image 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 docs image 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 File 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 docs image 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 docs image on the right side of the Text Builder, select Project Notebook (Notes) > 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, select the Common tab, and then 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 docs image 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:
      An older 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 docs image on the right side of the Text Builder, select Project Notebook (Notes) > Date [Sheet] > YYYYMMDD [Cell]. Click Save in the Text Builder.
    8. Select the Message Box activity added to the Designer panel and open its Properties panel:
      • From the Buttons drop-down menu, select YesNo.
      • Click Plus docs image on the right side of the ChosenButton field, then select Save for later use and give a name for the value, for example ChosenButton from Message Box FolderAlreadyExists.

        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. The user's choice is saved for later use in the automation.



  5. 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 Show Else to add the Else branch.
      • Click Plus docs image 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 docs image on the right side of the field on the left, and then select Use Saved Value > ChosenButton from Message Box FolderAlreadyExists. This selects the user's choice (the button clicked in 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 docs image 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 File 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 docs image 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 docs image on the right side of the Text Builder, select Project Notebook (Notes) > 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 True 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 docs image 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 docs image on the right side of the Text Builder, select Project Notebook (Notes) > 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 docs image 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 docs image on the right side of the From field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the Plus docs image on the right side of the Text Builder, select Project Notebook (Notes) > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Excel] Date!YYYYMMDD. Click Save in the Text Builder.
      • Click Plus docs image 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.



  6. Step 6: Save the attachments from the Outlook Search Folder.
    1. In the Activities panel, select the Mail tab, and then drag a Use Desktop Outlook App and drop it below the first If activity as the last activity in the project. The activity is added to the Designer panel.
    2. In the activity, the default email account is already selected in the Account field. If you want to use a different account, select it from the drop-down menu.
      In the Reference as field, leave the default value Outlook as the name by which to refer to the account in the automation.
    3. In the Activities panel, drag the For Each Email activity and drop it inside the Use Desktop Outlook App activity.
    4. In the For Each Email activity, click Plus docs image on the right side of the In emails 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 In emails 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 Email Attachments activity and drop it inside the For Each Email activity.
    6. In the Save Email Attachments activity:
      • Click Plus docs image on the right side of the From message field, and then select CurrentMail to indicate that the email whose attachments you want to save is the current email you are iterating through.
      • Click Plus docs image 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 docs image on the right side of the Text Builder, select Project Notebook (Notes) > 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.



  7. 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 File tab, and then drag a Create Folder activity and drop it below the Use Desktop Outlook App 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 docs image on the right side of the Folder name field, and then select Text. In the Text Builder, enter C:\MailAttachments_, then, from the Plus docs image on the right side of the Text Builder, select Project Notebook (Notes) > 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. In the Create File activity, click Plus docs image on the right side of the File location field, and then select Text. In the Text Builder, enter C:\MailAttachments_, then, from the Plus docs image menu on the right side of the Text Builder, select Project Notebook (Notes) > 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 docs image 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 docs image 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 docs image on the right side of the Text Builder, select Project Notebook (Notes) > 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 docs image on the right side of the Folder name field, and then select Text. In the Text Builder, enter C:\MailAttachments_, then, from the Plus docs image on the right side of the Text Builder, select Project Notebook (Notes) > 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.



  8. 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 docs image on the right side of the Folder field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the Plus docs image on the right side of the Text Builder, select Project Notebook (Notes) > 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 docs image on the right side of the Text field, and then select Text. In the Text Builder, enter FileName:, then, from the Plus docs image on the right side of the Text Builder, select CurrentFile > Name, then enter | LastModifiedDate:, then, from the Plus docs image on the right side of the Text Builder, select CurrentFile > Last Modified Date . The text should look like this: FileName: [CurrentFile] Name | LastModifiedDate: [CurrentFile] LastModifiedDate. Click Save in the Text Builder.
      • Click Plus docs image 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 docs image on the right side of the Text Builder, select Project Notebook (Notes) > 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 docs image on the right side of the From field, and then select CurrentFile > Full Path.
      • Click Plus docs image on the right side of the To field, and then select Text, In the Text Builder, enter C:\MailAttachments_, then, from the Plus docs image on the right side of the Text Builder, select Project Notebook (Notes) > 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.



    7. Click Save in the StudioX ribbon 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.

Was this page helpful?

Get The Help You Need
Learning RPA - Automation Courses
UiPath Community Forum
Uipath Logo White
Trust and Security
© 2005-2024 UiPath. All rights reserved.