• Ed G.

I Can Do That! - Add a Date (or anything, really) to a Filename.

Just today, a user in the Power Automate Community wanted to append a date to a filename while saving it to OneDrive for Business.

My initial thought was to simply add a couple of steps to get the current time, change it to a usable format, then append it to the original filename...easy, right?

Sometimes I race to respond because I am so eager to be helpful. I get excited thinking I can finally help someone and rush through to a solution without taking a moment to think things through. That is probably a hallmark of a 'good developer'. I'm getting there...baby steps, Bob.

My first solution looked like this thing to the left. Experts in the room have already figured out that if the filename was InsuranceClaims.xls, my new filename would be InsuranceClaims.xls_20200116_1802. This was less than ideal. So I asked the OP to stand by while I came up with a solution which parsed the extension from the filename, inserted the date, and put the extension back on...all (hopefully) without "code" as the OP was at the beginning of their expression-journey in Power Automate. ("Code" in quotes since folks have differing opinions on if expressions count or not. To me, they do...but that doesn't mean they have to be scary.

Spoiler alert: Sadly, I only knew how to do this with expressions so my best option was to break it down into easy-to-digest steps so that a new person, like myself, could approach it with a moderate level of confidence.

My one and only video (so far) walks through a similar situation where we want to split a string on a period, and return an array with the different elements on either side of the delimiter. That could be as simple as this:


split(triggerBody()['text'],'.')


Where triggerBody()['text'] is replaced with the dynamic value of the actual filename. Learning from past mistakes, though, I quickly decided this expression would be a problem if the filename had a period someplace other than the usual spot separating the name from the extension. As an example, things would totally get weird if the filename was instead, "Insurance.Claims.xls", so I wrapped that expression in a last(), which grabs the last element of an array. See? On my path to being a 'good developer' already. The updated expression looks like this:


last(split(triggerBody()['text'],'.'))


Since this will present me with the file extension ('xls', in my example), I can drop this into a Compose action to use again later. Best practices suggest renaming the step, so I called that action FileNameSuffix.

The prefix part gets strange, but if you're familiar with Excel...it will seem familiar in a moment. Using the same logic that I might use to approach the issue in Excel, I found equivalent functions to what I would do there. In Excel, the expression might look like this:

=LEFT(B2,FIND(".",B2)-1)


Which is basically grabbing everything on the left until you get to the period, and then back off once space. In Power Automate, we will use substring() instead of Left, indexOf() instead of Find, and our expression above instead of the period. We will grab everything up until the file extension, still backing up one space so we don't also grab the period. That expression looks like this:


substring(triggerBody()['text'], 0, add(indexof(triggerBody()['text'], last(split(triggerBody()['text'], '.'))), -1))


I know there's a prettier way to present that, but I also know I'd mess it up and someone might get cranky. Notice that I had to use add() because just having the -1 at the end didn't work. Also, I'm pretty certain I could replace the part in red with the output from my FileNameSuffix compose.

So now that we have the FileNamePrefix, FileNameSuffix, and a formatted date, we can put it all together.

These are all of the different pieces, and then the final "Create File" bit looks like this:

Note the highlighted underscore and period.

My usual caveat still applies...I know there are more elegant ways to do this, but these are the tools I know how to use, and I hope that it will help others be less intimidated with the idea of building expressions in Power Automate.


Get in, make mistakes, learn some things, and have fun! Try. Fail. Learn. Repeat.

©2018 by The Flying Polymath.