web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Email attachment to Sh...
Power Automate
Answered

Email attachment to SharePoint Folder based on the name of the file

(1) ShareShare
ReportReport
Posted on by 12

Desired Outcome: Part 1. copy attachments from email to specific folders on SharePoint based on the file name

 

i.e. 123_ABC_10.12.2023 -> saves to the folder 123_ABC

MsGoodie_0-1697123585067.png

using fx - 

split(triggerOutputs()?['headers']['FileName'], '/')
Error: Unable to process template language expressions for action 'Apply_to_each_2' at line '0' and column '0': 'The template language expression 'split(triggerOutputs()?['headers']['FileName'], '/')' cannot be evaluated because property 'FileName' doesn't exist, available properties are 'Cache-Control, Pragma, Transfer-Encoding, Location, Retry-After, Vary, Set-Cookie, Strict-Transport-Security, x-ms-request-id, X-Content-Type-Options, X-Frame-Options, x-ms-environment-id, Timing-Allow-Origin, x-ms-apihub-cached-response, x-ms-apihub-obo, Date, Content-Type, Expires'. Please see https://aka.ms/logicexpressions for usage details.'.
MsGoodie_1-1697123706447.png

 

MsGoodie_2-1697124110658.png

 

I need to put each attachment into the EmailAttachment array and then extract the filename but I am a little lost on how to do that. And how do I write an If/Else statment to look at every folder?

 

Categories:
I have the same question (0)
  • trice602 Profile Picture
    15,404 Super User 2025 Season 2 on at

    Hi @MsGoodie ,

     

    Here's a working flow to copy and implement. 

     

    trice602_0-1697127237505.png

     

    Trigger config:

     

    trice602_1-1697127304544.png

     

    First compose is to replace "/" with a "_" -- if it exists.  You can use this and build on replacing any characters you don't want in your folder names.

     

    replace(items('Apply_to_each')?['name'],'/','_')
     
    Second compose gets the file name after the last "." so the folder name doesn't contain .jpg or .xlsx
     
    split(outputs('Compose'), '.')[0]
     
    My folder path in create folder is from the output of Compose2, shown below
     
    trice602_3-1697127478599.png

     

    Lastly, I create the file in the newly created folder.  Please mark as a solution if this was helpful!

     

    trice602_4-1697127542366.png

     

     


     

     

  • MsGoodie Profile Picture
    12 on at

    @trice602 ,

    Tom thank you so much for your prompt response.

    2 Things...

    1. I don't need to create a folder, I need to look at the existing first 4 numbers to put it in the existing folder that matches those number. (so the flow as you laid it out works to create a folder but my folder structure already exists.)

    2. When I test the flow I am getting an error that the file name is too long.  
    Here is the error.

    The specified file or folder name is too long. The URL path for all files and folders must be 400 characters or less (and no more than 400 characters for any single file or folder name in the URL). Please type a shorter file or folder name.
    clientRequestId: 4bf57722-0cca-4db1-b14a-ead1a3cdac82
    serviceRequestId: 4bf57722-0cca-4db1-b14a-ead1a3cdac82

     

    MsGoodie_1-1697143900783.png

     



    I am wondering if we can truncate the name of the file that is being copied since we can't change the folder structure?

     

  • trice602 Profile Picture
    15,404 Super User 2025 Season 2 on at

    Hi @MsGoodie ,

     

    Understood.  Is the format always like this with two _'s?  If so, I can write an expression to get everything to the left of the second underscore.

     

     

     

    23_ABC_10.12.2023

  • Verified answer
    trice602 Profile Picture
    15,404 Super User 2025 Season 2 on at

    Hi @MsGoodie ,

     

    Please review this one.

     

    trice602_0-1697146609022.png

     

    expression for folder name:

     

    slice(items('Apply_to_each')?['name'], 0, nthIndexOf(items('Apply_to_each')?['name'], '_', 2))
     
    trice602_1-1697146659364.png

     

    And here's the dynamic folder path in create file

     

    trice602_2-1697146721033.png

     

     

    In this example, an attachment named 123_ABC_10.12.2023.xlsx was created in folder /123_ABC

     

     

  • MsGoodie Profile Picture
    12 on at

    Tom,

    Once again thank you for your help. I am still experiencing a similar error to the previous one - URL path 400 character limit - shorten file or folder name. I do not believe I've exceeded 400 characters.

     

    Would it be easier to copy the files to a specific SharePoint folder from the email and then copy it to the necessary location number in a secondary action? 

    Currently I am able to copy any that comes in email to a specific folder 

    MsGoodie_0-1697200588890.png

     

    I tried shortening the file name and I still got the same error.

    A new folder is being created but the file isn't writing.

    MsGoodie_1-1697200674230.png

     

     

  • trice602 Profile Picture
    15,404 Super User 2025 Season 2 on at

    Hi @MsGoodie ,

     

    Yeah, there's a lot of discussion about the MAXPATH limits and 400 is the number.  I'm not sure what options I would recommend here; if you truncate the path and file names too much, you lose the identification of the files.

     

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 501 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 323 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard