Let's POWER Automate

From no-code to low-code

Menu
  • Expressions
  • Filters
  • General
  • Application specific solutions
    • Dataverse
    • Excel
    • Forms
    • Outlook
    • Planner
    • Power Apps
    • SharePoint
    • Teams
  • Triggers
  • Ready to use solutions
    • Approval Process Template
    • Task Delegation App
    • The Ultimate Power Automate expressions cheat sheet
    • Power Automate HTTP requests to SharePoint cheat sheet
    • Power Automate HTTP requests to Graph API cheat sheet
  • ABOUT ME
  • Get help with your flow
  • POWER PLATFORM SERVICES
Menu

Move files to folders based on file name with Power Automate

Posted on March 24, 2024March 24, 2024 by Tom

“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]
Power Automate files folders name

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])
Power Automate files folders name

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.

Power Automate files folders name

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.


πŸš€ Master Power Automate

Join 2,000+ professionals getting actionable Power Automate tutorials, solutions, cheat sheets & tips every week.

No spam. Unsubscribe anytime.

1 thought on “Move files to folders based on file name with Power Automate”

  1. Septian Gusonela says:
    November 10, 2024 at 6:22 am

    nice post

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

πŸš€ Master Power Automate

Join 2,000+ professionals getting actionable Power Automate tutorials, solutions, cheat sheets & tips every week.

No spam. Unsubscribe anytime.

Still exchanging emails to get things approved?

Use the Approval Process Template and the Task Delegation App to skip the hard part and deploy an automated, fully functional approval solution on a SharePoint list in minutes! And then the next one, and the next one...

Approval Template Preview ✨ Learn more ✨

Turn ideas into flows with ease!

Grab the complete Power Automate Cheat Sheet Bundleβ€”everything you need to master expressions, SharePoint HTTP calls, and Graph API in Power Automate.

Cheat Sheet Bundle Preview ✨ Get the Cheat Sheets

Didn't find what you were looking for?
Need to adjust a solution to fit your needs?
Or would you just like to get an assistance from somebody with thousands of hours of experience with Power Automate?

Power Automate blogs worth visiting

Damien Bird
Dennis (Expiscornovus)
Paul Murana

©2025 Let's POWER Automate | Theme by SuperbThemes