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!

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.

To create the Flow, follow the steps outlined below:

  1. Click on New and select Automated — from blank.
  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.
  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.
  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.
  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.
  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.
  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.
  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.
  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.

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.

22 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?


  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?


  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.


    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.


  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?


    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.


  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


    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.


  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?




    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.


      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.


  7. @ Samuel Carman, I got the same problem when I put all the actions inside Initialize variable by adding an action. You have to start a new step between each and you will get the option.
    Now, this is awesome and it works great, except when I have a file that comes in at 10 pm or close to midnight, then it gets the date for the next day because it is based on UTC time. How do I get it match my current date and time?

    Liked by 1 person

    1. Hello @Ricardo Ramos and thank you for reading the post. I am glad to know you like it. For the date in the filename, you have to convert the utcNow() to your time zone. You can do the expression as follows:
      concat(outputs(‘Get_Attachment_Name’), ‘_’, convertFromUtc(utcNow(), ‘Eastern Standard Time’, ‘yyyyMMdd’), ‘.’, outputs(‘Get_Attachment_Extension’))

      You can replace Eastern Standard Time with the name of your time zone. Additionally, here’s a list of all Microsoft Time Zone Index:

      Please let me know if you get it to work or if you still have issues. Thanks.


  8. Hello @Fausto Capellan Jr, this is perfect. It works like a charm. I am fairly new at this and finding your blog was the best thing. Excellent description and details. Thanks so much for your help.

    Liked by 1 person

  9. Hi Fausto,

    Your steps worked awesome for me. The one issue I have is, I need to show previous dd instead of current. So DD should be DD-1, how will achieve this? Thanks much in advance.

    concat(outputs(‘Get_Attachment_Name’), ‘_’, formatDateTime(utcNow(), ‘yyyy_MM_dd’), ‘.’, outputs(‘Get_Attachment_Extension’))


    1. Hello Padma,

      You can craft the expression as follows:
      concat(outputs(‘Get_Attachment_Name’), ‘_’, addDays(utcNow(), -1, ‘yyyy_MM_dd’), ‘.’, outputs(‘Get_Attachment_Extension’)).

      Please try this and let me know.


Leave a Reply to Fausto Capellan Jr Cancel reply

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

You are commenting using your 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