Updating Filename in MS Flow (Power automate)

This is a walk through on how to update a filename while keeping the same file extension in MS Flow.

In this example we are going to assume the filename is saved in a Variable called “Full File Name”. The walkthrough will name a file to <filename>_1.<extension>

We will also assume that variables(‘AttachmentCounter’) is equal to “1”

For example: picture.jpg will be renamed to picture_1.jpg

tldr; the function is:
concat(concat(substring(variables('Full File Name'), 0, lastIndexOf(variables('Full File Name'), '.')), '_', string(variables('AttachmentCounter'))), '.', last(split(variables('Full File Name'),'.')))

Now lets break it down:

The inner most function is
substring(variables('Full File Name'), 0, lastIndexOf(variables('Full File Name') , '.')

This function gets the substring of the filename starting at index 0 and ending at the last index of a full stop (‘.’)

From our example (picture.jpg) this will return “picture”.

our next function is:
last(split(variables('Full File Name'),'.' )

This function will return the last element in an array which is split by a ‘.’. In our example it will return ‘.jpg’.

Now we just need to concat the results
concat(concat(picture '_', 1),'.', jpg)

and the result is