How-To #1: Rename Email Attachments using Microsoft Flow

After a long hiatus, I am back to blogging. With this post, I will start a How-To series to show how to do many different things in Flow, PowerApps, and other technologies. This series will cover things from basic to advanced. Let’s get started!

Scenario:
I have seen the request to rename email attachments quite a few times while browsing around the Microsoft Flow Community forums. While there have been similar requests with slightly different requirements, most of them use a similar approach to the one outlined below.

Note: all the actions below are used to describe the process in a simple way to facilitate learning. I will have a separate blog post to show an advanced method.

Solution:
To create the Flow, follow the steps outlined below:

  1. Click on New and select Automated — from blank.
    rename-1
  2. In the next screen, you can enter the name of your Flow, choose the trigger When a new email arrives and click on Create. You can also click on Skip so you can add the Flow name and choose your trigger on the Flow Builder.
    rename-2
  3. In the trigger, Inbox is the default folder, but you can change to a different one if desired. Click on Show advanced options and set the Has Attachment and Include Attachments properties to Yes. This will tell Flow to check for attachments and include them so they can be used in other actions throughout the Flow.
    rename-2.1
  4. For the next action, add an Initialize variable action. This variable will be used for the attachment renaming process. For the Name, I set it to varAttachmentName, but you can use any name you would like. For the Type, set it to String.
    rename-3
  5. For the next action, add a Set variable action. For the Name, select the name you entered in the Initialize variable action above. For the Value, select Attachments Name from the Dynamic content window.
    You will notice that once you select Attachments Name from the Dynamic content window, Flow adds an Apply to each action automatically. Why is that? That’s because Flow is expecting an array of attachments from the trigger, even if the email only has one attachment.
    rename-4
  6. For the next action, add a Compose action inside the Apply to each and rename it to Get Attachment Name. Click inside the Inputs field to display the Dynamic content window, select Expression, and build the expression below: first(split(variables('varAttachmentName'), '.'))
    How does this expression work? First, it uses the split() function to split the attachment name stored in the varAttachmentName variable at the dot, and then the first() function gets the content to the left of the dot.
    rename-5
  7. For the next action, add another Compose action inside the Apply to each and rename it to Get Attachment Extension. Click inside the Inputs field to display the Dynamic content window, select Expression, and build the expression below:
    last(split(variables('varAttachmentName'), '.'))
    This expression works similar to the expression above, with the exception that it’s using the last() function instead of the first(). The last() function gets the content to the right of the dot.
    rename-6
  8. For the next action, add another Compose action inside the Apply to each and rename it to New Attachment Name. Click inside the Inputs field to display the Dynamic content window, select Expression, and build the expression below:
    concat(outputs('Get_Attachment_Name'), '_', formatDateTime(utcNow(), 'yyyyMMdd'), '.', outputs('Get_Attachment_Extension'))
    This expression uses the concat() function to concatenate the following pieces: the attachment name from the Get Attachment Name action, an underscore (‘_’), the current date formatted as yyyyMMdd (formatDateTime(utcNow(), ‘yyyyMMdd’)), the dot for the extension (‘.’), and the attachment extension from the Get Attachment Extension action.
    rename-7
  9. For the last step, add a SharePoint – Create file action. Select the Site Address and the Folder Path. Then for the File Name, select the Output from the New Attachment Name action. And for the File Content, select the Attachments Content from the trigger.
    rename-8

Send yourself an email with a few attachments in it to test it out. This process will get the attachments, rename them, and then save them with the new name in the selected path.

If you have any questions, feedback or have a Flow idea you would like to tackle, please do not hesitate to post a comment below or reach out via Twitter.
Thank you for reading.

2 thoughts on “How-To #1: Rename Email Attachments using Microsoft Flow

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s