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.

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

  1. i love this step by step tutorial! by far the easiest one I’ve seen to follow. I am having trouble when I want my attachment name to be fixed and not pulled from the current file name. How do I modify step 6 to do this?

    Like

  2. hello I followed the same steps more or less but anyways my excel file is corrupted when ı changed the name. have you faced this issue before?

    Like

  3. Thank you this was great! But is there a way to rename the file completely in this example it takes the existing name and concatenates the date. for example file attachment name is dog.xls would like to rename to cat.xls.

    Like

    1. Hi Melissa,
      Thank you for the comment. Since the original requirements were to always append the current date to the file name, you can always customize to your needs. For example, if you want to use a completely different file name, you can skip steps 6 – 8 and add the file name in step 9.

      Like

  4. hi Fausto, great info! i’m actually trying to remove a time/date stamp on a file. Basically renaming the file so it is a consistent name. I’ve tried to simply set the File Name in the “Create File” action instead of using Dynamic Content. The Flow works and the file is created with the specified name BUT, the format of the CSV file appears to be corrupted. Any suggestions?

    Like

    1. Hi Raymund,
      Thank you for your comment. So I can understand your issue better, the file name change is working, but the file content is not coming as expected? Can you please provide information on the actions in your flow?
      Thank you.

      Like

  5. Fausto, Similar problem to Raymund. File is a cvs file. Followed all steps except 6,7, and 8. File is saved with name change but always corrupt? Thoughts? Thanks Dave

    Like

    1. Hi Dave,

      I just tested the flow with a .csv file and it worked as expected. Something you can try is to wrap the file content value inside an expression like this: base64ToBinary(items(‘Apply_to_each’)?[‘contentBytes’]) to see if that makes a difference. Please test that and let me know.
      Thank you.

      Like

  6. Hi,
    I am not getting the Output option at the end when creating the sharepoint file after following all of the steps.
    Do you have any idea why?

    Thanks,

    Sam

    Like

    1. Hi Samuel,

      Did you do the Compose action with the concat() expression in it? If you can, hit me up on Twitter and send me screenshots of your flow so I can take a look at it. My Twitter handle is @fcapellanjr.

      Thank you.

      Like

      1. Hi,
        Yeah, I’ve had to do it slightly differently as I’m after a different output. I can see after the flow has ran that part of it has my desired output but there’s no way of getting int the final ‘create file’ section.

        Like

Leave a Reply to Dave Smith Cancel 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