Flow: Strip Dots and Special Characters from File Names

Hi there Flow Fans.

A while back I posted in the "Flow Cookbook" with a post called "Save Outlook Attachments And Guard Against Duplicate File Name Overwrite" and here on my blog a post called "Excel Formats Only | Save New Email Attachments to SharePoint". Whilst this How-to blog post works very well, I do mention this:

"Below uses a split to allow us to isolate the file type or extension as well as the file name. It breaks into 2 minus the dot (period). This will work perfectly, assuming there are no dots in the file name itself."

There have been a lot of users in the community contact me saying that these processes work very well for them, although a couple who have mentioned they have "dots" (periods) in their file names hence there can be issues with the flow. This post will address that, so if we consider a file name called "this=is&a.spreadsheet.03.19.xlsx" then we will split this into 2 whilst stripping out any potential problem characters, dots, ampersands, equals signs etc.

So we started with …

File Name: this=is&a.spreadsheet.03.19.xlsx

We will be left with …

File Name: thisisaspreadsheet0319

File Extension: .xlsx

We can then incorporate this process to the Flow I have referenced at the beginning or any other Flow that has issues relating to file names (or any string really) contaning dots, or special characters.

In order to start off this simulated process, I will start with a button. This just means it is easy to test and helps you get the concept of what we are actually doing.

Let's simulate the file name using a "Compose", renamed to "Compose File Name With Dots".

Note: There will be several "Compose" actions used in this Flow, just rename them exactly as you see them and you will be able to copy and paste the code.

Note: You will probably replace this in production by targeting which ever value is relevant. Attachment Name perhaps when working with Outlook Email.

this=is&a.spreadsheet.03.19.xlsx

Now let's Split of just the file extension (xlsx in this case)

last(split(outputs('Compose_File_Name_With_Dots'), '.'))

Let's create the actual file extension by adding the dot with the concat() function.

concat('.', outputs('Compose_Split_At_Dot'))

This next step will now target the remainder of the file name, the bit we will want to clean up or sanitise.

split(outputs('Compose_File_Name_With_Dots'), outputs('Compose_File_Extension'))[0]

Now to initialise a few variables that will allow us to strip out the dots, as well as any other special character whilst we are at it.

createArray('.','@','ß','²','³','µ','´','°','^','=','(',')','&','$','§', '~','#','%','*',':','<','>','?','/','|',' ')

We will have to loop through our problematic file name in order to clean out the dots, or special characters so let's use "Apply to each", name it accordingly perhaps.

Now some "Set variable" actions, again helpfully named accordingly

replace(variables('CleanFileName'), item(), ")

This now means, when we use a final "Compose" action called "Compose CleanFileName" so we can easily see the result of the variable "CleanFileName" we have a sanitised string.

Result.

Result.

Try it for yourself, rebuild this flow and enter varying values in "Compose File Name With Dots". As long as the character you want to strip out is contained within "Initialize Variable InvalidCharacters" then it will be removed from the string. Should you want to add additionla charaters to this array or perhaps, remove characters from the array then please do.

Consider the following …

and this …

createArray('.','@','ß','²','³','µ','´','°','^','=','(',')','&','$','§', '~','#','%','*',':','<','>','?','/','|',' ')

If it were just the dot (period) you were wanting to clean then maybe you could have an array set like this …

createArray('.')

I will let you all take it from here but I hope you find this useful.

Thanks, Alan