Skip to main content

Notifications

Community site session details
Power Automate - Building Flows
Answered

How to automatically save email attachments to OneDrive WITHOUT overwriting existing files

Like (0) ShareShare
ReportReport
Posted on 3 Oct 2021 00:25:37 by

When automatically saving attachments from email into OneDrive, using the Power Automate Create File function, the new file will automatically overwrite the existing file.  Create File does not append a serial number or otherwise handle the duplication in a lossless way. I don't see any easy way to automatically assign a unique name to new files, so that the existing ones are not overwritten.

I assume  one could get the names of all the files in the folder (hundreds? thousands?), check the list for duplicate file names, and if necessary add a serial number to generate a unique name. That's a lot more Power Automate code than I am prepared to learn, write, and debug.

One can reduce the problem somewhat by prepending to the filename the time the file was received, but that results in long clunky file names, and does not successfully handle multiple attachments from the same email that just happen to have the same file name.  

Is there any solution to this issue?  It seems very limiting to the general usefulness of the email-to-OneDrive flow.

  • Community Power Platform Member Profile Picture
    on 05 Oct 2021 at 20:11:03
    Re: How to automatically save email attachments to OneDrive WITHOUT overwriting existing files

    And a slightly larger, slightly different .docx file comes through corrupted.  This does not work.  I'm bagging it.

     

  • Community Power Platform Member Profile Picture
    on 05 Oct 2021 at 18:44:48
    Re: How to automatically save email attachments to OneDrive WITHOUT overwriting existing files

    This has been putting 4 byte files into the Attachments folder.  I think that there must be something broken in the initial Create File operation.

  • Community Power Platform Member Profile Picture
    on 05 Oct 2021 at 04:33:07
    Re: How to automatically save email attachments to OneDrive WITHOUT overwriting existing files

    That's great, thanks.   I have also tried adding a Send Email operation, Configured To Run After if the Move fails.  All it does is send me an email if the move fails - not nearly as good as your solution.  

    bcgirton_0-1633408281264.png

     

  • Verified answer
    VJR Profile Picture
    7,635 on 05 Oct 2021 at 04:08:52
    Re: How to automatically save email attachments to OneDrive WITHOUT overwriting existing files

    Hi @Anonymous 

     

    You need to add two branches. One if the move was a success and another if it was not.

     

    - Add another move after the first move

    This left branch is in case if the first move failed

    This time give a new name (maybe a datetimestamp suffix).

    The second move is to rename the file along with the move.

     

    VJR_0-1633406316793.png

     

    - Select the plus sign below the original move and choose "Add a parallel branch"

    Now you have the right hand side branch. Choose what you want to do in case the original move was a success.

     

    - See the red box marked with the dotted arrow in the above screenshot on the LHS.

    To achieve that click on the three dots in the second move -> Then click "Configure run after" -> Then select as below

     

    VJR_1-1633406650039.png

    This will tell the flow to run the LHS branch only when the first move has failed.

    You can also select "is skipped" or "has timed out" depending on the needs of your project requirements.

     

    It will be difficult for me to show the error handling here :). So you might want take a look at some videos and articles on this topic.

     

    Hope this should get you going.

  • Community Power Platform Member Profile Picture
    on 05 Oct 2021 at 03:46:20
    Re: How to automatically save email attachments to OneDrive WITHOUT overwriting existing files

    If there is already a document with the same name in the Attachments folder, then the Move Or Rename (Overwrite = No) does not move the file.

    {"statusCode":400,"headers":{"Pragma":"no-cache","Transfer-Encoding":"chunked","x-ms-request-id":"5ed77b20-025d-49e6-8aa1-10d91d914c59","Timing-Allow-Origin":"*","x-ms-apihub-cached-response":"true","Cache-Control":"no-cache","Date":"Tue, 05 Oct 2021 03:41:13 GMT","X-AspNet-Version":"4.0.30319","X-Powered-By":"ASP.NET","Content-Type":"application/json","Expires":"-1","Content-Length":"187"},"body":{"status":400,"message":"Destination file '/Attachments/Document.docx' already exists. To overwrite this file, set overwrite to 'true'.","source":"api.connectorp.svc.ms"}}

    Unfortunately, if the Power Automate window is not open, and I don't look in the Attachments/&temp subfolder to see if there is a document stuck there, then I won't know about the failure.   

  • Community Power Platform Member Profile Picture
    on 05 Oct 2021 at 03:36:31
    Re: How to automatically save email attachments to OneDrive WITHOUT overwriting existing files

    The two Move functions differ only in how the file to be moved is referenced.  Move or rename a file uses the file ID as a handle, whereas Move or rename a file using a path uses the file path.

     

  • VJR Profile Picture
    7,635 on 05 Oct 2021 at 03:28:16
    Re: How to automatically save email attachments to OneDrive WITHOUT overwriting existing files

    Hi @Anonymous 

    Check the outputs of the move action and see whether it gives output dynamic content that says move was a success or not. 

  • Community Power Platform Member Profile Picture
    on 05 Oct 2021 at 03:22:48
    Re: How to automatically save email attachments to OneDrive WITHOUT overwriting existing files

    I sent two messages with the same attachments.  The first time, the file was moved from the /Attachments/&temp/ subfolder to the /Attachments/ folder.  The second time, the file was not moved; it was left in the /Attachments/&temp/ subfolder.  The move without overwrite instruction did not overwrite the first file, but there was no other action such as an error message.

  • VJR Profile Picture
    7,635 on 05 Oct 2021 at 03:16:02
    Re: How to automatically save email attachments to OneDrive WITHOUT overwriting existing files

    Hi @Anonymous 

     

    The move simply did not occur; the file was left in the temp folder.  The flow failed silently. 

    The move worked out for me smoothly.

    So please double check on path.

  • Community Power Platform Member Profile Picture
    on 05 Oct 2021 at 03:07:06
    Re: How to automatically save email attachments to OneDrive WITHOUT overwriting existing files

    I guess I got confused. You wrote:

     

    Be noted that if a file with the same name already exists there then this action shows the below message.

    So handle it as per your requirements. 

     

    I interpreted "handle it" as manually dealing with the error when the "below message" was shown.  However, I never saw the "below message" or any other indication of trouble.  The move simply did not occur; the file was left in the temp folder.  The flow failed silently. 

     

    I agree about the date / time stamp.  If I inserted a delay of 1 second in the Apply To Each loop, no two file names would ever be the same.  I don't know how to do that, but no doubt there must be a way. 

     

    The resulting file names would still be long and ugly.  Making all file names that way is not a satisfactory solution, but maybe it is a useful way to handle duplications. If the Move with no overwrite fails, prepending the date/time stamp before the file name would make it unique.  There would have to be an error handling routine; I don't know how to catch the error.

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,743 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,079 Most Valuable Professional

Leaderboard