web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Move named files to su...
Power Automate
Unanswered

Move named files to subfolders that contain a portion of file name

(0) ShareShare
ReportReport
Posted on by 14

Rookie user and I searched the forum and came close to a solution but not hitting the target.

 

I have a folder with old scanned patient charts. It has 75k files in it all named based off the patient ID, for example "12345".

 

I need to move these into the new systems patient folders, but they are all named "LastName-FirstName-12345".

 

I'd like to find a way for Power Automate to look at the folder with the files and the folder with all the new sub-folders and find that "12345" match and move the file to the matched subfolder.

I have the same question (0)
  • mmonline Profile Picture
    169 on at

    I need a bit more information. The 75k files.. you say the name is based on the Patient ID.

     

    However, I suspect the patient ID is part of the file name, not the entire file name. However, if the files are, in fact, based solely on a numeric patient ID, the flow below will work.

     

    Flow overview (or is that, Floverview*:

    ¯\_(ツ)_/¯

     

    1. Create a table with patient IDs folder paths
      1. Get subfolders in folder (where your new patient folders are)
      2. Create a data table named, PatientFolderTable
      3. For each folder
        1. Get the folder name
        2. Split using the "-" (based on your above naming convention)
          This will create a list with 3 values
          Value [0] - last name
          Value [1] - first name
          Value [2] - patient ID
        3. Create variables for those values (patient id is the important one)
        4. Insert these values into the above created data table
      4. Get all the files to copy
      5. For Each file
        1. Set Filename to %CurrentFile.NameWithoutExtension%
          Note: that would be the patient ID if the files are only named patient ID.
          See my caveats below the flow image 
        2. Search for the file name (patient ID) in the PatientFolderTable
        3. Using the row for any found values, get the FolderPath as CopyToPath variable
        4. Move the file

    FLOW IMAGE

    mmonline_0-1689034578755.png

     

    CAVEATS

    I assume the 75k files are some variation of file names that contain the patient ID. This will require that you do a bit more parsing to arrive at the proper patient ID. If you give me more information (sample file names - as long as no HIPAA issues exist. You can dummy names but a sampling of what the files name contain.

     

    From this we can modify the flow accordingly. There are times that such operations are more easily done via code - Run Javascript, Python, or VBScript, with a file name, in order to deal with all the file name variations.

     

    In any case, I hope this is helpful. If you can provide more filename information, I'll try to provide a more complete answer.

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    The above solution by @mmonline seems like it would do the trick, but it is overly complicated in my opinion.

    All you really need to do is get all files, build a loop that iterates through those files, find the target folder for each specific file and move the file.

     

    Here's a sample of such flow, which is much more simple:

    Agnius_0-1689044443758.png

    Here's a snippet you can copy and paste directly into your PAD designer to have the actions automatically created for you:

    Folder.GetFiles Folder: $'''C:\\RPA\\Input''' FileFilter: $'''*''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Files=> Files
    LOOP FOREACH File IN Files
     Folder.GetSubfolders Folder: $'''C:\\RPA\\Output''' FolderFilter: $'''*-%File.NameWithoutExtension%''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Subfolders=> Folders
     File.Move Files: File Destination: Folders[0] IfFileExists: File.IfExists.DoNothing MovedFiles=> MovedFiles
    END

    Note you will need to change the directories from "C:\RPA\Input" to your actual directory where the files are stored and from "C:\RPA\Output" to the directory with the sub-folders. 

     

    Please note the caveats regarding file naming that were mentioned by @mmonline do apply here, too.

     

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution.
    If you like my response, please give it a Thumbs Up.

    If you are interested in Power Automate, you might want to follow me on LinkedIn at https://www.linkedin.com/in/agnius-bartninkas/

     

  • jotty Profile Picture
    14 on at

    Yes, the files are in fact just the patient id. I've attached a screenshot. That is just how they were exported out of the old system. I won't provide a screenshot of the new system folder for those HIPAA reasons, but the subfolder naming structure is exactly what I originally put, "LastName-FirstName-12345".

    Capture.PNG
  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Well then, both solutions above will work for your case. Mine is a bit shorter and easier to implement, but they both do the trick.

    -------------------------------------------------------------------------

    If I have answered your question, please mark it as the preferred solution.

    If you like my response, please give it a Thumbs Up.

    If you are interested in Power Automate, you might want to follow me on LinkedIn at https://www.linkedin.com/in/agnius-bartninkas/

     

  • jotty Profile Picture
    14 on at

    Using your method in a test environment. Worked perfectly. However implementing in live I get the screenshotted error on the move files step.

    Capture.PNG
  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    An index out of range error indicates that you are trying to access an item in an empty list. I don't use any indexes in my proposed solution above. Can you share a screenshot of the first 4 actions of your flow?

     

  • jotty Profile Picture
    14 on at

    Only thing I've done is change the folder directories. Is it possible the Y: directory is too generic and needs a subdirectory?

     

    FUNCTION Main_copy GLOBAL
    Folder.GetFiles Folder: $'''C:\\Users\\jott\\Documents\\Fortis''' FileFilter: $'''*''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Files=> Files
    LOOP FOREACH File IN Files
    Folder.GetSubfolders Folder: $'''Y:\\''' FolderFilter: $'''*-%File.NameWithoutExtension%''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Subfolders=> Folders
    File.Move Files: File Destination: Folders[0] IfFileExists: File.IfExists.DoNothing MovedFiles=> MovedFiles
    END
    END FUNCTION

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    It's failing because of the %Folders[0]% part, indicating the folder does not exist (nothing returned by the Get subfolders in folder action). Try checking if %Folders% is not empty before doing the move.

    -------------------------------------------------------------------------

    If I have answered your question, please mark it as the preferred solution.

    If you like my response, please give it a Thumbs Up.

    If you are interested in Power Automate, you might want to follow me on LinkedIn at https://www.linkedin.com/in/agnius-bartninkas/

     

  • jotty Profile Picture
    14 on at

    That is it. No folders are pulling in. There are just over 200k subfolders in that directory. Wondering if there are just too many folders to load and go through before it errors out.

     

    In my test I just took 20 files and made 20 subfolders to imitate the scenario and it worked great. So with only changing the directories to the real directories is it failing.

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    I don't think that it errors out. Is the directory a local one, or is it a network directory? Perhaps there's some sort of a sync issue?

    -------------------------------------------------------------------------

    If I have answered your question, please mark it as the preferred solution.

    If you like my response, please give it a Thumbs Up.

    If you are interested in Power Automate, you might want to follow me on LinkedIn at https://www.linkedin.com/in/agnius-bartninkas/

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 248 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 204

#3
Haque Profile Picture

Haque 179

Last 30 days Overall leaderboard