“I’d like to add a date to the name of the file received by an email, how can I inject it in there with Power Automate?”
When you store email attachments in a SharePoint document library, you might encounter files with the same file name. If the names are not unique, and you don’t handle this situation, the flow will fail.
You’ve got three ways how to solve this. The first one is to update the stored file. Keep the file including its version history and replace only its content. The second option is to replace the file. Remove the existing one, and upload the new one. And the third one is renaming the file to make the name unique. You can add the date at the beginning of the file name, or you can add it at the end. And that’s the topic of this post – adding date at the end of a file name.
Get the file name and the extension
To add a date in the file name, you must split it into two parts. The first part is the file name without the extension, and the second part is the extension. The date will then go between these two parts.
Get the file name
Let’s get the file name first. The approach will be similar as when splitting any string, starting with the split(…) expression to split the whole file name by a dot.
Since the last item in the array will be always the file extension, take(…) all the pieces excluding the last one. That means the length(…) -1.
take(split(<attachment name>,'.'),add(length(split(<attachment name>,'.')),-1))
Then just join(…) the file name again into a string in case there were any other dots in the file name.
join(take(split(<attachment name>,'.'),add(length(split(<attachment name>,'.')),-1)),'.')
Note: < … > is a placeholder, replace it with the file name including the < and >.
That’s the file name without the extension.
Get the file extension
The same approach can be used also for the file extension. Start by splitting it again by the dot. But this time you don’t need to calculate the length, you can simply take only the last(…) piece of the array = the extension.
Build the new file name
Once you have the file name without extension, and the extension, you can build the final file name. Add the current date in between with the concat(…) and utcNow(…) expressions. Don’t forget to add the dot before the extension!
concat(<name without extension>, '_', utcNow(), '.', <extension>)
With the two ‘Compose’ actions it might look as below. You can also add some formatting to the date if you don’t want the full ISO string.
concat(outputs('Compose'), '_', utcNow(), '.', outputs('Compose_2'))
If you use Power Automate to create files in the same SharePoint library, and you expect they might have the same name, it’s a good idea to add a date to the file name. Split the file name into two pieces, the file name without extension and the extension, and inject the date/time in between. Such file name will be always unique and you don’t have to deal with updating/replacing the existing files.
And if you encounter corrupted attachments, take a look on the previous post.