Determine unique name for file to save to SharePoint folder with just 3 flow actions

Posted by

When you upload a file to a folder in SharePoint, the system will check whether the file name is unique in that folder. If not, the system will present you the option to replace or keep both. In the latter case the file name will get an index number, like shown in the following image.

Now I want to have the same “experience” when automating the upload of e-mail attachments to a SharePoint folder. Some of the file names of these attachments won’t be unique, and will be overwritten by default. How can I prevent this in Power Automate flow?

I’ve created a very simple flow with 3 actions. For sake of readability and to demo the result I added 3 extra Compose actions. Here is the overview of the flow:

The attachments have a name attribute that I have to split in the name (without extension) and the extension, to be able to concatenate the unique file name with an index number. I use the following expressions in the first 2 supporting Compose actions:


The essential part of the flow is the Do until loop. The limits are default and not changed. We use the ‘Status code’ from the output of the ‘Get file metadata using path’ action to check for the 404 code (file not found).

With the ‘Get file metadata using path’ action we will check if the file name already exists in the SharePoint folder. This action is using the Graph API under the hood.
The action will fail when the file is not found. To make sure the flow run continues we set the ‘Configure run after’ of the ‘Compose File Name’ action to: is successful, has failed.

I use the iterationIndexes function (available in Until loops) as the counter, so I don’t have to use -slow- variables. Depending on the iterationIndexes value I construct the path and file name with the following expression:

concat('/library/folder/',if(equals(iterationIndexes('Do_Until_NotFound'),0),item()?['name'],concat(outputs('Compose_Name'),' (',iterationIndexes('Do_Until_NotFound'),')',outputs('Compose_Extension'))))

If the iterationIndexes value is 0 then the original file name is used, else the iterationIndexes value is added as index number.

The iterationIndexes value is used to set the index number in the file name to make it unique.

The iterationIndexes value is only available within the Do until loop, so I need the ‘Compose File Name’ action to save the unique file name for use beyond the Do until loop. The expression I use is.

if(equals(iterationIndexes('Do_Until_NotFound'),0),item()?['name'],concat(outputs('Compose_Name'),' (',iterationIndexes('Do_Until_NotFound'),')',outputs('Compose_Extension')))

Now we can use the outputs(‘Compose_File_Name’) in the Create file action to set the unique file name and save it to the folder in SharePoint. In this demo the ‘Compose Unique File Name’ action is used instead but can be removed.
The expressions in the first 2 supporting Compose actions can be nested in the expression of the 2 action within the Do until loop, so you can remove them from the flow if you like.
That brings the flow solution back to 3 actions, I like it!


Leave a Reply

Your email address will not be published. Required fields are marked *