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:
- Click on New and select Automated — from blank.
- 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.
- 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.
- 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.
- 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.
- 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:
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.
- 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:
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.
- 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.
- 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.
47 thoughts on “How-To #1: Rename Email Attachments using Microsoft Flow”
Thanks for this. Great step by step guide. I now have a far better understanding of how to create well structured ‘Flows’.
Hi Kim. Thank you for your comment, and I’m glad this blog post has been helpful. Please reach out if there’s anything you would like covered in a future post.
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?
If the file name will always be static, you can bypass steps 6 – 8 and add the file name in step 9.
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?
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.
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?
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.
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.
Great info Fausto!
LikeLiked by 1 person
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?
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?
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
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.
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?
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.
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.
@ 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?
LikeLiked by 1 person
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.
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.
LikeLiked by 1 person
Hi Ricardo. I’m happy to know you got it working. If there’s anything you would like to see covered in another blog post, please let me know and I’ll put it together.
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’))
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.
Sorry for the late reply, it worked for me. Thanks very much.
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
Hi Joseph Buta,
I just tested the flow with multiple attachments and it worked as expected. Can you please send me screenshots of your flow via Twitter? My Twitter handle is @fcapellanjr.
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’?
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.
I tried the flow but the file name did not change.
When looking at the Run history:
5. Set variable – looks correct.
Output Name: newname
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,
Can you please send me some screenshots of your flow via Twitter? You can reach me at @fcapellanjr. Thank you and I look forward to hearing from you so we can solve that issue.
Thanks for sharing this. My attachment (CSV file) stores with a null value in it and nothing else.
Can you please provide more details on your flow and where you think it’s failing? Thanks.
At a high level I have three steps;
1. When a new email arrives in a shared mailbox (V2)
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”.
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 firstname.lastname@example.org
I can send you print screens
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?
You can skip the following steps:
* 5 (Don’t use the Set variable action)
Then in step 9, you can pass the static name directly in the File Name field. I hope that helps.
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
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!
Thanks a lot for this tutorial. I am new to power automate. Can you please help me to add one more step to this to send an email notification once the file renamed and uploaded to share point folder successfully.
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.
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.
Can you please post the expression you are using for the file name?
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!
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?
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
+ When a new email arrives
+ Initialise variable
+ Apply to each
[ SET VARIABLE
[[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)
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”
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
Please send me an email to email@example.com so I can send you a copy of the original flow I created. That way you can dissect it and test it out.