cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jleung
Level: Powered On

Manipulate filename from email attachment

I used the SharePoint create file action to save file to SharePoint library from email attachments. It overwrites the file if it already exist. I then try to add with timestamp to the filename but only as prefix because the "Name" dynamic content from email attachment includes file extension. Ideally, the new filename should be [filename]-[timestamp].[file extension]

 

I am still new to use Flow. Anyone has suggestion or work around for this? Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Manipulate filename from email attachment

Hi @jleung,

 

This is the approach I took to achieve your desidred outcome. I will make the assumption that you want to create the files in the SharePoint library from attachments every time a new email arrives.This may be a lengthy post since it includes screenshots.

 

  1. The first step is "When a new email arrives". Make sure the options Has Attachments and Include Attachments are set to Yes.
    FileName-1.png

  2. In the next two steps, add an "Initialize variable". I used the first variable for the AttachmentName and the second for the Timestamp.
    FileName-2.png

  3. In the next step, add an "Apply to each". In the output field, select Attachments from the Dynamic content.
    FileName-3.png

  4. Within the "Apply to each", you will add all the remaining steps for the Flow.
  5. The first step is "Set variable". For the Name, select AttachmentName and for the Value, select the Name(Attachment name) from the Dynamic content.
    FileName-4.png

  6. The next step is another "Set variable". For the Name, select Timestamp and for the Value, build the Expression below. This Expression uses the formatDateTime function to format the utcNow() date time to the following format: 'yyyyMMdd-hhmmss'.
    formatDateTime(utcNow(),'yyyMMdd-hhmmss')

    FileName-5.png

  7. The next step is a "Data Operations - Compose" - (Compose-GetFileName), which is used to extract the File Name of the attachment without the extension by using the Expression below. The Expression splits the AttachmentName variable at the period(.) and retrieves all the text before it.
    first(split(variables('AttachmentName'),'.'))

    FileName-6.png

  8. The next step is another "Data Operations - Compose" - (Compose-GetFileExtension). This is similar to the step above, but the Expression retrieves the file extension after the period(.)
    last(split(variables('AttachmentName'),'.'))

    FileName-7.png

  9. The next step is another "Data Operations - Compose" - (Compose-ConcatFullFileName). This step concatenates the output from the Compose-GetFileName step, the Timestamp variable, and the output from the Compose-GetFileExtension step. The Expression below has all the details for the concat funcion.
    concat(outputs('Compose-GetFileName'),'-',variables('Timestamp'),'.',outputs('Compose-GetFileExtension'))

    FileName-8.png

  10. And the final step is the SharePoint "Create file". Select the Site Address and the Folder Path. For the File Name, select the output from the Compose-ConcatFullFileName. And for the File Content, select the Content(Attachment content) from the Dynamic content. Set the name of your Flow, save it, and test it.
    FileName-9.png

Please let me know if it works or if you have any questions.

 

Regards,

Fausto Capellan, Jr


Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


19 REPLIES 19
Super User
Super User

Re: Manipulate filename from email attachment

Hi @jleung,

 

This is the approach I took to achieve your desidred outcome. I will make the assumption that you want to create the files in the SharePoint library from attachments every time a new email arrives.This may be a lengthy post since it includes screenshots.

 

  1. The first step is "When a new email arrives". Make sure the options Has Attachments and Include Attachments are set to Yes.
    FileName-1.png

  2. In the next two steps, add an "Initialize variable". I used the first variable for the AttachmentName and the second for the Timestamp.
    FileName-2.png

  3. In the next step, add an "Apply to each". In the output field, select Attachments from the Dynamic content.
    FileName-3.png

  4. Within the "Apply to each", you will add all the remaining steps for the Flow.
  5. The first step is "Set variable". For the Name, select AttachmentName and for the Value, select the Name(Attachment name) from the Dynamic content.
    FileName-4.png

  6. The next step is another "Set variable". For the Name, select Timestamp and for the Value, build the Expression below. This Expression uses the formatDateTime function to format the utcNow() date time to the following format: 'yyyyMMdd-hhmmss'.
    formatDateTime(utcNow(),'yyyMMdd-hhmmss')

    FileName-5.png

  7. The next step is a "Data Operations - Compose" - (Compose-GetFileName), which is used to extract the File Name of the attachment without the extension by using the Expression below. The Expression splits the AttachmentName variable at the period(.) and retrieves all the text before it.
    first(split(variables('AttachmentName'),'.'))

    FileName-6.png

  8. The next step is another "Data Operations - Compose" - (Compose-GetFileExtension). This is similar to the step above, but the Expression retrieves the file extension after the period(.)
    last(split(variables('AttachmentName'),'.'))

    FileName-7.png

  9. The next step is another "Data Operations - Compose" - (Compose-ConcatFullFileName). This step concatenates the output from the Compose-GetFileName step, the Timestamp variable, and the output from the Compose-GetFileExtension step. The Expression below has all the details for the concat funcion.
    concat(outputs('Compose-GetFileName'),'-',variables('Timestamp'),'.',outputs('Compose-GetFileExtension'))

    FileName-8.png

  10. And the final step is the SharePoint "Create file". Select the Site Address and the Folder Path. For the File Name, select the output from the Compose-ConcatFullFileName. And for the File Content, select the Content(Attachment content) from the Dynamic content. Set the name of your Flow, save it, and test it.
    FileName-9.png

Please let me know if it works or if you have any questions.

 

Regards,

Fausto Capellan, Jr


Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


jleung
Level: Powered On

Re: Manipulate filename from email attachment

Thanks so much for your approach. Very detail explanation too and really helpful!

 

I modified the expression in the (Compose-GetFileName) step so that if the filename includes a dot (.), it will also include it.

 

The expression is substring(variables('AttachmentName'),0,lastIndexOf(variables('AttachmentName'),'.'))

 

 

Super User
Super User

Re: Manipulate filename from email attachment

Hi @jleung,

 

Glad to hear the approach helped you in completing your Flow.

 

Regards,

Fausto Capellan, Jr


Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


KevinStef
Level: Powered On

Re: Manipulate filename from email attachment

Excellent solution and works as expected.

 

I was wondering how I could add the ability to change the actual file name in addtion to adding the date. For example the attachment comes in as book1.xls, in the end I'd like it to post to the SP library as Monday Report-(Todays Date).xls

 

Thanks,

 

Kevin

Super User
Super User

Re: Manipulate filename from email attachment

Hi @KevinStef

 

You can check out step 9 and follow a similar approach using the concat() function. You can set any number of parameters to this function to complete the expression you need for your new file name. Please let me know if you have any questions.


Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


PORTOCAR
Level: Powered On

Re: Manipulate filename from email attachment

Bravo! Exactly what I needed!  Thank you. Would be great if MSFT would simplify this with a simple rename step as the current steps are too many.

RVKONDRA
Level: Powered On

Re: Manipulate filename from email attachment

Hi Fausto, i tried below format of floe to get attachment and save it on share point document library, but in step 10 in file name field, i am not able to see output option in dynamic content. Please advise.

Super User
Super User

Re: Manipulate filename from email attachment

Hi @RVKONDRA

The file name field in step 10 actually comes from the Compose action in step 9. Were you able to create that step asn shown above? If so, can you please post a screenshot so I can take a look at it?

 

Thanks,

Fausto Capellan, Jr


Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


RVKONDRA
Level: Powered On

Re: Manipulate filename from email attachment

Hi Fausto, thanks for coming back. I am unable to put screen shots in this message. Do you know how to put screen shots when you reply?

 

 

RVKONDRA
Level: Powered On

Re: Manipulate filename from email attachment

Hi Fausto, have you received my message with screen shots of my flow?

 

RVKondra

Super User
Super User

Re: Manipulate filename from email attachment

Hi @RVKONDRA

 

Yes, I did. Please check your inbox when you have a chance.


Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


rmattewada
Level: Power Up

Re: Manipulate filename from email attachment

very detail oriented - thanks for the solution.
JBarretta
Level: Power Up

Re: Manipulate filename from email attachment

I know this is an old thread, but wanted to say thank you very much! I've spent a solid 4 hours trying to figure out how to manipulate names of email attachments with no luck. This worked like a charm.

JBarretta
Level: Power Up

Re: Manipulate filename from email attachment

 
Highlighted
Kgrange
Level: Powered On

Re: Manipulate filename from email attachment

Any ideas on how this can be modified to get the extension if the file name is "Purchase order - 0012171 Rev .1.pdf" as the solution provided would result in the extension being .1.pdf

 

I am tring to save only the .pdf files and not the jpg or token files that come with emails but more and more are arriving with extra full stops in the filename that make the current solution report incorrectly as false.

Super User
Super User

Re: Manipulate filename from email attachment

Hi @Kgrange 

 

This post by a community staff shows all the steps needed to achieve your solution. Please try it and let me know.

https://powerusers.microsoft.com/t5/General-Flow-Discussion/Split-period-separated-strings-and-conca...


Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


loganl
Level: Powered On

Re: Manipulate filename from email attachment

Brilliant stuff @faustocapellanj - is there any way you could manipulate an attachment name from a Sharepoint list so that it's accessible as an OFT?

I've tried just about every approach I can think of, but each time I try I get a permissions error. I'm trying to inject Powerapps info into an email, send it to a Sharepoint list then return it as an attachment, where it gets emailed to the original author. 


Capture.PNG

 

Super User
Super User

Re: Manipulate filename from email attachment

Hi @loganl 

Can you please post a screenshot of the Flow you've built so I can take a look? Also, please let me know if I'm right: you're manipulating information thru PowerApps and storing it in a SharePoint list. Once that item is created, you need to get it, create a new attachment, and send it to the person that created the item?

Thanks.


Fausto Capellan, Jr


Did I answer your question? Mark my post as a solution! Did my response help? Please give it a thumbs up!

Proud to be a Flownaut!


loganl
Level: Powered On

Re: Manipulate filename from email attachment

That's correct, @faustocapellanj - though I've tried so many different methods now that I'm beginning to believe MS doesn't support manipulating OFTs without getting this error. Here's a stripped-down version of what I'm attempting to do. I understand the mechanics, and can build a workable flow for this, except I can't open the final result!

The only way I know how to directly create the OFT file is simply adding the "OFT" extension to the attachment. I suspect that's where I'm screwing things up.


1.png2.png

Your guidance is so appreciated! I'd love to use a format other than OFT, but this will be for a MS sales contest, and the sellers require a brand-approved template. 

Helpful resources

Announcements
firstImage

Microsoft Business Applications Virtual Launch Event

Watch the event on demand for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Power Platform 2019 Release Wave 2 Plan

Power Platform 2019 Release Wave 2 Plan

Features releasing from October 2019 through March 2020.

thirdimage

Flow Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

sixthImage

Power Platform World Tour

Find out where you can attend!

seventhimage

Webinars & Video Gallery

Watch & learn from the Flow Community Video Gallery!

Users Online
Currently online: 256 members 4,462 guests
Please welcome our newest community members: