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 / How to automatically s...
Power Automate
Unanswered

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

(0) ShareShare
ReportReport
Posted on 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.

Categories:
I have the same question (0)
  • VJR Profile Picture
    7,635 on at

    Hi @Anonymous 

     

    Download those attachments to a temporary location and then use the "Move or rename file" to the desired location by setting the overwrite to No.

     

    VJR_1-1633233490839.png

     

    There is also an action called "Move or rename file using path". Haven't checked the difference between these two. But the above one worked out for me.

     

     

    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. 

     

    VJR_0-1633232770771.png

     

     

  • Community Power Platform Member Profile Picture
    on at

    Thanks.  I can give that a try.  Human exception handling works. Do you know if there is any way to deal with that error in the code?  Ideally, the program would do something like automatically append a number to the duplicated filename, and then increment the number each time the error recurred until a file name was found that was not already in use.

  • Community Power Platform Member Profile Picture
    on at

    bcgirton_0-1633401885937.png

    The above flow ALMOST worked.  When the second message came with the duplicate file name, the last step, the Move with no overwrite, failed.  However, I did not get any error message.

  • VJR Profile Picture
    7,635 on at

    Hi @Anonymous ,

     

    I am not sure what you meant by "human exception handling".

    The solution that I have shared with you does not involve any human intervention.

    Let me know if you need any further clarification on that.

     

    Regarding your another question you can prefix or suffix the datetimestamp (with seconds) to the filename so that they will never be duplicate. <filename dd-MM-yyyy hh_mm_ss.pdf>.

    Make sure to give a delay of at least 1 second so that two files will never be created within the same second.

    You mentioned about time when the file arrives in the original post, but giving the date is also important to make the filename unique. Else there could be a possibility of files with same time on two different dates. But the names will never repeat if dates are included with year and time with the seconds.

  • Community Power Platform Member Profile Picture
    on at

    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.

  • VJR Profile Picture
    7,635 on at

    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 at

    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 at

    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 at

    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.

     

  • Community Power Platform Member Profile Picture
    on at

    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.   

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 538 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 405 Moderator

#3
abm abm Profile Picture

abm abm 252 Most Valuable Professional

Last 30 days Overall leaderboard