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

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.

49 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

      1. So, if I want the name of attachment (.xlsx) saved in Share Point to be static and I bypass 6-8 and create my flow, does it mean the file in SharePoint will get overwritten every time I get an email with the specified attachment?

        Like

      2. Another question – I built the flow excluding steps 6-8 and it ran but the .xlsx file in the email got saved as only ‘file’. I am unable to understand how do I add the file extension to my flow, so it gets saved as .xlsx?
        I am a novice in this area, please help me understand this better. Thanks.

        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

  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:
      https://support.microsoft.com/en-us/help/973627/microsoft-time-zone-index-values

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

      Like

  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’))

    Like

    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.

      Like

  10. Hi Fausto. This doesn’t seem to work if an email had multiple attachments. i.e. if two files were attached, it just concatenates the two file names together, and only creates one file. Example Below

    [Test_file_1_20201017.pdf_,_Test_File_2_20201017.xlsx]

    Like

  11. Hi Fausto! I love this article. The email I have generated from Salesforce, unfortunately, contains a graphic! When I removed steps 6-8, it saved the graphic on top of the data file… so that was a no go. Is there a way that I can change “Get_Attachment_Name” to a filename that I designate… that way it keeps my XLSX and just dumps the graphic file and anything else it designates with different extension into the Onedrive folder I did… basically… how can I change ‘Get_Attachment Name’ to a static “MyFileNameHere” with the related extension that comes from the ‘Get_Attachment_Extension’?

    Like

    1. Hello Jamiers,

      If the email contains that image file that you don’t need, you can add a Condition after the Get Attachment Extension action to check for the file extension you really need. Condition could say if file extension equals .xlsx, perform all the actions in the Yes branch. And for the No branch, you can either terminate the flow or take a different action. I hope this points in the right direction.

      Like

  12. Dear Fausto,

    I tried the flow but the file name did not change.

    When looking at the Run history:
    On
    5. Set variable – looks correct.
    Output Name: newname
    Value: oldfilename.xlsx

    However, on
    6. Get Attachment Name,
    the output is the same as the input.
    // my formula is : first(split(variables(‘newname’), ‘.’))

    What am I doing wrong?
    BTW I tried the newname with and without extension. Both times it didn’t change the name.

    Thank you in advance,

    Nikolay

    Like

  13. At a high level I have three steps;
    1. When a new email arrives in a shared mailbox (V2)
    2.Initialize variable
    3. ForEach Attachment

    ForEach Attachment has the following steps
    – Set variable
    – Get Attachment Name
    – Get Attachment Extention
    – New Attachment Name
    – Create file (Sharepoint)

    Inside Create file, I have site address and folder path. File Name is set to Outputs from New Attachment Name and File content is set to Attachments Content.

    Also, ForEach Statement has first step as “Select an output from previous steps” and selection is “Attachments”.

    Like

  14. hi Fausto

    I have a question on dates format

    I did a flow from Siri to Outlook/ I found on youtube and I adapted another flow – create calendar event/ but the flow always put the date on the current day, not where I want it.

    I tried with formatdatetime but the flow didn’t recognize the expression.

    I don’t have twitter
    my email is nls_76_99@yahoo.com

    I can send you print screens

    Like

  15. Hi Fausto,

    Thanks for this, super helpful.
    I had one follow up question:

    I have to change the name of the file from “GivenName.csv” to “Input_File.csv”.
    Can you provide some insight on how to achieve this?

    Like

  16. Hi Fausto,

    Thank you very much for the splendid tutorial, simple but very informative.

    I just have a question on, Instead of adding the date to the file name, is there a way it can copy the subject line of the email as its file name?

    Really appreciate if you can help me on this

    Neil

    Like

    1. Hello Kristofer,

      If you want to replace the Date with Subject in the file name, you can do an expression as follows:
      concat(outputs(‘Get_Attachment_Name’), ‘_’, triggerBody()?[‘Subject’], ‘.’, outputs(‘Get_Attachment_Extension’))

      However, if it’s just need the Subject you need as the file name, you can do an expression as follows:
      concat(triggerBody()?[‘Subject’], ‘.’, outputs(‘Get_Attachment_Extension’))

      Please let me know if that works. Thank you!

      Like

    1. Hello Aparna,

      You can add an Outlook – Send an email action after the Create file action. Keep in mind that if your flow is creating files from multiple attachments, an email will be sent for each file. Please let me know if you have any questions. And thank you for reading.

      Like

  17. Hi Fausto,

    Great post! It was much helpful.
    However I’m doing slightly different here, im adding subject line string to the file attachment. Everything works fine, but the final output is adding “_”(underscore) to the beginning and end of the filename which causing the invalid filetype.

    Can you please help me with that.

    Like

  18. Hi Fausto,

    I’ve followed the instructions to the letter but get a Flow Checker error: “Correct to include a valid reference to ‘Get_Attachment_Name’ for the input parameter(s) of action ‘New_Attachment_Name’.”

    I’m just hoping to get the attachment and rename it to, for example, Filename.xls_yyyymmdd in a OneDrive folder

    I replaced your step of saving to OneDrive over SharePoint, but either way, it does not recognise the ‘output’ variable shown in step 9; as in, I’m unable to select the ‘Output’ to set in the ‘Create File’ step. It doesn’t seem to have been created (as per the error message) I’m hoping you can help!

    Thankyou
    Steve

    Like

    1. Hi Steve,

      Double-check the file name format you want to use since it shows the date will be in the extension and that will give you issues opening the file. As for the New Attachment Name expression, can you please post it here so I can see what it looks like?
      Thank you!

      Like

  19. Hi Fausto,

    Appreciate the quick reply! Here’s the expression (copied from your page)
    As I’m unable to post images of the flow, here’s a text representation of my flow with the following key:

    + represents a new step
    [ ] represents a variable operation within an apply to all step

    FLOW:
    + When a new email arrives
    + Initialise variable
    + Apply to each
    [ SET VARIABLE
    Name: VarAttachmentsname

    [[Compose: GET ATTACHMENT NAME
    Inputs: first(split(variables(‘varAttachmentName’), ‘.’))

    [[[Compose: GET ATTACHMENT EXTENSION
    Inputs: last(split(variables(‘varAttachmentName’), ‘.’))

    [[[[Compose: NEW ATTACHMENT NAME
    Inputs: concat(outputs(‘Get_Attachment_Name’), ‘_’, formatDateTime(utcNow(), ‘yyyyMMdd’), ‘.’, outputs(‘Get_Attachment_Extension’))
    ]
    + CREATE FILE (OneDrive)
    File Name:
    File Content”

    END FLOW

    Ideally I’ll remove the datetime section so attachments are simply named “Filename_yyyymmdd”, which I guess would look like this:

    concat(outputs(‘Get_Attachment_Name’), ‘_’, ‘yyyyMMdd’), ‘.’, outputs(‘Get_Attachment_Extension’))

    They will most likely be .xls/x files, but it’s good to be able to download and suffix any type of file with “_mmmmyydd”

    Thanks!
    Steve

    Like

  20. Hi Fausto, I typed out a looong response but it doesn’t seem to have posted; in the interim I figured it out and it works perfectly in saving and renaming the file in my OneDrive…..except that when I try to open the file I get an error saying that the ‘File format or file extension is not valid’. Each file is only 1kb.

    For context, I’m sending a small .xlsx file and the files, once saved by the Flow in my OneDrive look like “Filename 01_20220701.xlsx and they are of type ‘Microsoft Excel Worksheet’.

    I’ve copied the Compose terms directly from your instructions so I’m unsure where I may have gone wrong

    Thanks
    Steve

    Like

    1. Hello,

      To get the domain of the sender email, you can use the following expression: last(split(triggerOutputs()?[‘body/from’], ‘@’))

      As for the date of reception, you can add a Compose action and add the Received Time value from the Dynamic content. Once you have the Compose action set up, add another compose action and do an expression like this: formatDateTime(outputs(‘Compose’), ‘yyyyMMddhhmmss’). Thank you!

      Like

Leave a reply to Padma Priya Balapa Cancel reply