“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.
split(<attachment name>,'.')
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.
last(split(<attachment name>,'.'))
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'))
Summary
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.
This works great! One issue I have found is that even though it’s creating a new file, the other columns are taking on the same metadata as the last entry. How do I make sure that the new added file will default to the values that I have set in SharePoint? Example would be Status should be “Not Started” but it’s coming in as “Completed” and the People field is populated as the last value entered instead of being blank as my defaults are set in SPO. I cant figure out where this is happening, do you have an idea?
Do you guys really think this is a realistic way to do this in 2024. This should be as easy as [datetime]+[filename]. How dumb to have to break apart the file name and data/time and then create 5 variables to hold this information and then piece it back together. Microsoft is so lame.