cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
pnr
Frequent Visitor

Overwrite file if it already exist

I am having a flow where I convert some Word documents to PDF, when the Word document are being updated or created. Then it move the PDF to another location. This is all fine, but sometimes a PDF is already on the location when the flow creates a new one (step Create file 2), and then the flow fails 😐 Isn't it possible to make so that it will overwrite the existing file, if it is already there? 

pnr_0-1612188320814.png

 

23 REPLIES 23
stephankline
Community Support
Community Support

Hello,

 

Thank you very much for the screenshot and general idea of how you'd like the flow to operate.

 

In looking at what you have so far and your proposed result, it looks as though you may be missing a Condition control to check if the pdf already exists in the destination.  The basic logic looking something like this:

 

  • If file exists equals "Yes", then delete the found file with the recently converted pdf. 
  • If file does not exist, then create file (as you already have set up in an Action).

 

To see a similar working version of this for attachments, I was able to locate another thread that used a different trigger, but the end result matching up with what’d you like to do:

 

Save attachment to One Drive and OVERWRITE an exis... - Power Platform Community (microsoft.com)

 

Community Support Team - Stephan Kline

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

tom_riha
Super User
Super User

Hello @pnr ,

the 'Create file' action doesn't have a possibility to replace an existing file. I'd say you've got two options:

  1. If a file with the name already exists, delete the current file and create a new one as proposed by @stephankline .
  2. Create one more "shadow" document library on the site, create the file in the new library, and then use 'Move file' action to move the file to the main library. 'Move file' will allow you to replace the file if it already exists = Power Automate will take care of the deletion/creation of the new file in the main library.

image.png



[ If I have answered your question, please Accept the post as a solution. ]
[ If you like my response, please give it a Thumbs Up. ]

[ I also blog about Power Automate solutions even for non-IT people. ]
JanHogerwerf
Frequent Visitor

I am using the replace option and it somehow works. 

 

But what I don't like is that it moves the old file to the recycle bin instead of overwriting.

 

Some files get replaced a lot and then get many files in my recyble bin. 

 

Can it not just overwrite the existing file? And use version history if you want to see the file that has been replaced?

 

Thank you!

Hello @JanHogerwerf ,

you can, but it's a bit more work: https://tomriha.com/how-to-update-not-replace-an-existing-file-in-power-automate/



[ If I have answered your question, please Accept the post as a solution. ]
[ If you like my response, please give it a Thumbs Up. ]

[ I also blog about Power Automate solutions even for non-IT people. ]

@tom_riha I know but was trying to avoid the extra work by using the standard function 😛

I can't even count how many hours I wasted by trying to avoid extra work... 😁



[ If I have answered your question, please Accept the post as a solution. ]
[ If you like my response, please give it a Thumbs Up. ]

[ I also blog about Power Automate solutions even for non-IT people. ]
RajMalot
Frequent Visitor

I have a similar issue updating a PDF file attachment in list library. 

 

1. The update file doesn't update the file. Is this method only works for Document Library?

RajMalot_0-1662560562716.png

 

2. If you Delete the attachment, and Add Attachment. That results in file moved message.

Am I doing something wrong?

 

As far as I know updating file works.

 

Check if file exists --> If not create file. If it does exist use update. That way existing file is not sent to recycle bin.

RajMalot
Frequent Visitor

Thank you, I do check if file exist.

RajMalot_0-1662562327020.png

 

I was unable to make the update file method to work with list attachment. Hence I tried other options suggested and it works fine now.

 

1. Get list item attachments

2. Delete the list attachment if exists

3. Add attachment to list item

 

RajMalot_0-1662654055222.png

 

Think you need to add the 'get attachment content'  step and add the attachment content from there

HEATFreight
Post Prodigy
Post Prodigy

The solution to this problem is to use "Create File" instead of "Add Attachment".

This blog says you need to disable "Allow chunking" and you will be able to overwrite existing SharePoint files, but in my testing that is not necessary. You do not need to disable chunking.

 

All you need to do is use "Create file" and the file will be created or overwritten seamlessly without having to check if it exists and delete it first! "Add Attachment" gives an error if the file already exists, but "Create File" works whether it exists or not. "Add Attachment" is specific to attaching files to list items, but "Create File" works for any folder in SharePoint. "Lists" is a folder and each list is itself a folder and the attachments to a list is a folder and each list item is a folder.

Therefore, if you want the file to attach to a list item in a SharePoint list, the syntax for "Folder Path" in your "Create File" action is as follows:

/Lists/<List Name>/Attachments/<List Item Id>

 

Thanks.

 

From my experience the create file used to fail if the file already existed.

 

But will check it out anyway.

 

I found out that if you use "CreateFile" to add an attachment to a SharePoint list item, you must already have some attachment there or it will fail because the "attachments" folder does not exist yet. My workaround was just to add a small placeholder image in Power Apps to every record that requires attachments.

Then I run "CreateFile" using the below code to designate the target list item as a folder:

/Lists/<List Name>/Attachments/<List Item Id>

 
You must have chunking disabled! On the "CreateFile" action, click settings and disable chunking.

If you don't disable chunking, then it's possible the "CreateFile" will fail to overwrite the existing file.

As long as two criteria are met, "CreateFile" for an existing file overwrites the existing file no problem:
1) - the SP list item must already have some file attached to it

2) - chunking must be disabled or you will see failures with large files. larger files will run as chunked, and that will fail to overwrite.

JanHogerwerf
Frequent Visitor

Hmm my question was related to creating a file in a folder, not to adding an attachment to a list item (I would use update item for that).

 

But maybe these are same actions I don't know.

It's the same thing.

List items ARE folders.

For some folder in a document library, everything I've said is still true. Just make sure the path is correct and follow my instructions, and "CreateFile" overwrites successfully.

And by the way, my rule #1 above only applies if the folder does not exist yet. So in the case of a SharePoint list item, the attachments to that list item are saved in a folder, and that folder does not exist until you add attachments to the list item, so "AddAttachments" in Power Automate works even if no files have been added yet, because that action is apparently able to create the attachments folder, but "CreateFile" does not. I have not figured out how to create the folder first, so I add a placeholder file in my canvas app before the Power Automate flow runs, then when the flow runs "CreateFile" succeeds because the folder already exists because the canvas app has already added a placeholder file.

I think in your case, all you need is for the destination folder to already exist, and then "CreateFile" should overwrite existing files successfully, assuming "chunking" is disabled in the settings for that "CreateFile".

I should clarify that, in my use case, I need the "CreateFile" action to work regardless of whether or not the file exists yet. On the first run, the placeholder file we added before "CreateFile" preps the target location by creating the folder that my file needs to go to, but on subsequent runs, we skip the placeholder file because the folder already exists, and we skip deleting all existing conflicts because any file we send with "CreateFile" will overwrite conflicts.

The result is that our flow to simultaneously upload several files to SharePoint runs twice as fast as before because we're not wasting time unnecessarily deleting older versions of the files or, on the first run, looking for older versions to delete which don't exist yet.

Yeah i know all these connectors are somehow related and can be converted to each other.

 

But don't want to do too much tweaking of these connecters. It is supposed to be a low-code solution aiming to have a large user base. So not having to do difficult things and learn by trial and error but commonly used functions being standard available.

Helpful resources

Announcements
Power Automate News & Announcements

Power Automate News & Announcements

Keep up to date with current events and community announcements in the Power Automate community.

Community Calls Conversations

Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (4,577)