“I’d like to organise files into folders based on part of the file name, can I use Power Automate flow to move them for me?”
Organising files is one of the tasks that can take a lot of time to do manually, yet it’s quite easy to automate. It has simple rules – identify something in the file and move it to the correct folder. For this article it’ll be something in the file name. If the files have a specific piece of string in the name, e.g. some ID, the flow will move them to a corresponding folder.
List only files in the library
Before you move the files, you’ll have to get them. You can organise them either right away, e.g. once you get them by email, or later if you already have an unorganised library.
In this example let’s take all the files in the library. Start with ‘Get files (properties only)’ and select the files. Since folders are also considered “files” from SharePoint point of view, let’s filter them out – you don’t want to move any folders. Get only files whose ContentType is not Folder.
ContentType ne 'Folder'
Extract the folder name from the file name
If the folder name is in the file name, the first step is to extract it from the name. Split the file name as explained in the previous article, and select the “folder name” part. My file name starts with invoice ID followed by customer name, e.g. 20220101-Justo_Tomas Riha.pdf.
If I wanted to create folder based on the invoice ID, I could extract the first part from the file name.
split(<fileName>, '-')[0]
e.g.
split(items('Apply_to_each_-_file_to_move')?['{FilenameWithExtension}'],'-')[0]
But organising by invoice ID doesn’t make much sense as it’s unique. The customer name on the other side is not, so let’s extract the customer name. I’ll split it by the underscore and take the first part, then split again by dash and take the second part.
split(split(<fileName>, '_')[0], '-')[0]
e.g.
split(split(items('Apply_to_each_-_file_to_move')?['{FilenameWithExtension}'], '_')[0], '-')[1]
Move the file to the folder
Once you extract the folder name from the file name, you can use it to actually move the file. Instead of selecting the destination folder from the available choices, use the folder name expression. Include also the library name in the path, e.g.
concat('/Shared Documents/', split(split(items('Apply_to_each_-_file_to_move')?['{FilenameWithExtension}'], '_')[0], '-')[1])
There’s one small problem though – the ‘Move file’ action won’t work if the folder doesn’t exist…
Create the folder and try again
Unlike the ‘Create file’ action, ‘Move file’ doesn’t create folders. The destination folder must already exist if you want to move the file.
Since checking the folder before each move would complicate the flow, you can use the same workaround as when updating file. Check if the action fails and if so, create the folder.
The folder name is again the expression extracted from the file name.
Once you create the folder you can try to move the file again. Just copy/paste the original ‘Move file’ action at the end.
Summary
As you can see, it’s quite a simple flow that can save you a lot of work. With so many documents in every organisation, it’s good to have all the files organised in some way. Even if it’s just to move them into various folders based on the files name, this Power Automate flow can be a huge improvement.