cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jotty
Regular Visitor

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

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.

16 REPLIES 16

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
Most Valuable Professional
Most Valuable Professional

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/

 

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

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".

Agnius
Most Valuable Professional
Most Valuable Professional

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/

 

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

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

Agnius
Most Valuable Professional
Most Valuable Professional

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?

 

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

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
Most Valuable Professional
Most Valuable Professional

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/

 

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

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
Most Valuable Professional
Most Valuable Professional

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/

 

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

I initially thought that as it was a network directory when I started. Then I installed PA on the server itself so it would be local. Still run into the issue. I know when I open the directory specifically it does take a minute or two to load everything that is inside it, so a sync issue still could be the cause.

Agnius
Most Valuable Professional
Most Valuable Professional

I guess that could be the issue, yes. I would suggest trying to force the machine to load the directory before trying to get the sub-folders. You can try to use the Run application action to launch explorer.exe with the directory as the argument like this:

System.RunApplication.RunApplicationAndWaitToLoad ApplicationPath: $'''explorer.exe''' CommandLineArguments: $'''C:\\RPA''' WindowStyle: System.ProcessWindowStyle.Normal Timeout: 600 ProcessId=> AppProcessId WindowHandle=> WindowHandle

This will open the folder in Windows Explorer, which should force Windows to load all those sub-folders. You can then use Close window to close it after you're done.

 

I don't have a folder at hand with so many sub-folders that my machine would take a while to load it all, so I cannot reproduce the exact situation, but you might need to also use Wait for window content and wait for some kind of an element appearing or disappearing to make sure the sub-folders have in fact loaded. Or, at the very least, add some fixed waiting time with a Wait action. And then try to use the Get sub-folders in folder action.

 

I think you only need to do this once and not for each item in your loop. Open the explorer and then run the entire loop. This should 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/

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

I wonder, given that it is a one-time file move, whether using something like PowerShell might be more efficient. I haven't had to work with that many folders but PowerShell may provide a more robust tool in this case.

I messed with PowerShell but the logic was having issues differentiating between like numerical numbers for example: 51455, 151455, 251455. It would never find the 5 digit numbered folder, but always would find the 6 digit match and throw the file in. That days of frustration led me here. Still unsuccessful though.

Another idea. I just tested this for retrieving the path to copy to.

 

There are limits on reading Excel data and/or creating data tables. In effect, reading a large folder listing (or file listing) is creating an array or data table. This may be the source of the challenge.

 

What I just tested was creating an Excel file with the folder listing in question:

 

  • From a command prompt, in the network folder, run:
    "Dir *. /b >allFolders.txt"

    This will get you a directory listing with just folder names (none of the meta data)

  • Copy this into an Excel file, starting at row two.
  • Create headings on the first row (freeze this row)
    • Col A: Foldername
    • Col B: PatientID
    • Col C: FullPath
  • In Column B put the following formula:
    =RIGHT(A2,LEN(A2)-FIND("_",A2,FIND("_",A2)+1))

    This will extract the patientID from the folder name.

  • In Column C put the following formula:
    ="P:\DataFolder\" & B2
    Where "P:\DataFolder\" is the root folder of the destination folders.

    You could create this folder at runtime from column A but I just included it for simplicity.
  • Copy the formulas to all the subsequent rows of the Excel file.
    This gives you a "database" of the patient IDs and paths.

You can now:

  • Create a SQL connection to the Excel file
  • Get your files and get the file name (without extension)
  • Use that name in a SQL query, searching on Patient ID.
  • The resulting row, if found, will have your path. 
  • Use the file and the returned path to move the file.

The Excel connection string s/b:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Data\Dev\PowerAutomate\PatientidsToFolder.xlsx;Extended Properties='Excel 12.0 Xml;HDR=YES';

 

Where: C:\Data\Dev\PowerAutomate\PatientidsToFolder.xlsx is the path to your Excel file/database.

 

Here is a flow reading my Excel sheet with 200,000 rows.

mmonline_0-1689185538621.png

 

 

Database.Connect ConnectionString: $'''Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Data\\Dev\\PowerAutomate\\PatientidsToFolder.xlsx;Extended Properties=\'Excel 12.0 Xml;HDR=YES\'''' Connection=> SQLConnection
SET PatientID TO 54343114808
Database.ExecuteSqlStatement.Execute Connection: SQLConnection Statement: $'''Select Foldername, PatientID, FullPath FROM [PatientFolders$]
WHERE PatientID = \'%PatientID%\'''' Timeout: 30 Result=> FolderData
LOOP FOREACH CurrentItem IN FolderData
SET FullPath TO CurrentItem[2]
END
Database.Close Connection: SQLConnection

 

 

NOTES

  • It takes about 1 second to return the SQL query. Moving the data to Access to SQL would result in a faster query... but more work and the real performance improvement is probably nominal. Moving files will take that much time anyway.

  • Yes.. it takes a little pre-work. But really, it only took about 10 minutes to set this sheet up.
     
  • @Agnius is a pretty smart guy. He may have an improvement/optimization on this. But, it should work.

 

The result of a query - looking at the variable FolderData in my flow.

mmonline_1-1689185828639.png

 

The Excel File: I didn't have 200,000 names handy so I simply added the row number to the end of an earlier row and copied it across 200,000 rows.

mmonline_2-1689186016073.png

 

Let us know if you are able to craft a solution.

 

Agnius
Most Valuable Professional
Most Valuable Professional

This is a pretty good idea. I would just add that running SQL queries to Excel requires the following driver to be installed on the machine: https://www.microsoft.com/en-us/download/details.aspx?id=54920

 

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

Helpful resources

Announcements

Community will be READ ONLY July 16th, 5p PDT -July 22nd

Dear Community Members,   We'd like to let you know of an upcoming change to the community platform: starting July 16th, the platform will transition to a READ ONLY mode until July 22nd.   During this period, members will not be able to Kudo, Comment, or Reply to any posts.   On July 22nd, please be on the lookout for a message sent to the email address registered on your community profile. This email is crucial as it will contain your unique code and link to register for the new platform encompassing all of the communities.   What to Expect in the New Community: A more unified experience where all products, including Power Apps, Power Automate, Copilot Studio, and Power Pages, will be accessible from one community.Community Blogs that you can syndicate and link to for automatic updates. We appreciate your understanding and cooperation during this transition. Stay tuned for the exciting new features and a seamless community experience ahead!

Summer of Solutions | Week 4 Results | Winners will be posted on July 24th

We are excited to announce the Summer of Solutions Challenge!    This challenge is kicking off on Monday, June 17th and will run for (4) weeks.  The challenge is open to all Power Platform (Power Apps, Power Automate, Copilot Studio & Power Pages) community members. We invite you to participate in a quest to provide solutions to as many questions as you can. Answers can be provided in all the communities.    Entry Period: This Challenge will consist of four weekly Entry Periods as follows (each an “Entry Period”)   - 12:00 a.m. PT on June 17, 2024 – 11:59 p.m. PT on June 23, 2024 - 12:00 a.m. PT on June 24, 2024 – 11:59 p.m. PT on June 30, 2024 - 12:00 a.m. PT on July 1, 2024 – 11:59 p.m. PT on July 7, 2024 - 12:00 a.m. PT on July 8, 2024 – 11:59 p.m. PT on July 14, 2024   Entries will be eligible for the Entry Period in which they are received and will not carryover to subsequent weekly entry periods.  You must enter into each weekly Entry Period separately.   How to Enter: We invite you to participate in a quest to provide "Accepted Solutions" to as many questions as you can. Answers can be provided in all the communities. Users must provide a solution which can be an “Accepted Solution” in the Forums in all of the communities and there are no limits to the number of “Accepted Solutions” that a member can provide for entries in this challenge, but each entry must be substantially unique and different.    Winner Selection and Prizes: At the end of each week, we will list the top ten (10) Community users which will consist of: 5 Community Members & 5 Super Users and they will advance to the final drawing. We will post each week in the News & Announcements the top 10 Solution providers.  At the end of the challenge, we will add all of the top 10 weekly names and enter them into a random drawing.  Then we will randomly select ten (10) winners (5 Community Members & 5 Super Users) from among all eligible entrants received across all weekly Entry Periods to receive the prize listed below. If a winner declines, we will draw again at random for the next winner.  A user will only be able to win once overall. If they are drawn multiple times, another user will be drawn at random.  Individuals will be contacted before the announcement with the opportunity to claim or deny the prize.  Once all of the winners have been notified, we will post in the News & Announcements of each community with the list of winners.   Each winner will receive one (1) Pass to the Power Platform Conference in Las Vegas, Sep. 18-20, 2024 ($1800 value). NOTE: Prize is for conference attendance only and any other costs such as airfare, lodging, transportation, and food are the sole responsibility of the winner. Tickets are not transferable to any other party or to next year’s event.   ** PLEASE SEE THE ATTACHED RULES for this CHALLENGE**   Week 1 Results: Congratulations to the Week 1 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Community MembersNumber SolutionsSuper UsersNumber Solutions Deenuji 9 @NathanAlvares24  17 @Anil_g  7 @ManishSolanki  13 @eetuRobo  5 @David_MA  10 @VishnuReddy1997  5 @SpongYe  9JhonatanOB19932 (tie) @Nived_Nambiar  8 @maltie  2 (tie)   @PA-Noob  2 (tie)   @LukeMcG  2 (tie)   @tgut03  2 (tie)       Week 2 Results: Congratulations to the Week 2 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 2: Community MembersSolutionsSuper UsersSolutionsPower Automate  @Deenuji  12@ManishSolanki 19 @Anil_g  10 @NathanAlvares24  17 @VishnuReddy1997  6 @Expiscornovus  10 @Tjan  5 @Nived_Nambiar  10 @eetuRobo  3 @SudeepGhatakNZ 8     Week 3 Results: Congratulations to the Week 3 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 3:Community MembersSolutionsSuper UsersSolutionsPower Automate Deenuji32ManishSolanki55VishnuReddy199724NathanAlvares2444Anil_g22SudeepGhatakNZ40eetuRobo18Nived_Nambiar28Tjan8David_MA22   Week 4 Results: Congratulations to the Week 4 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 4:Community MembersSolutionsSuper UsersSolutionsPower Automate Deenuji11FLMike31Sayan11ManishSolanki16VishnuReddy199710creativeopinion14Akshansh-Sharma3SudeepGhatakNZ7claudiovc2CFernandes5 misc2Nived_Nambiar5 Usernametwice232rzaneti5 eetuRobo2   Anil_g2   SharonS2  

Check Out | 2024 Release Wave 2 Plans for Microsoft Dynamics 365 and Microsoft Power Platform

On July 16, 2024, we published the 2024 release wave 2 plans for Microsoft Dynamics 365 and Microsoft Power Platform. These plans are a compilation of the new capabilities planned to be released between October 2024 to March 2025. This release introduces a wealth of new features designed to enhance customer understanding and improve overall user experience, showcasing our dedication to driving digital transformation for our customers and partners.    The upcoming wave is centered around utilizing advanced AI and Microsoft Copilot technologies to enhance user productivity and streamline operations across diverse business applications. These enhancements include intelligent automation, AI-powered insights, and immersive user experiences that are designed to break down barriers between data, insights, and individuals. Watch a summary of the release highlights.    Discover the latest features that empower organizations to operate more efficiently and adaptively. From AI-driven sales insights and customer service enhancements to predictive analytics in supply chain management and autonomous financial processes, the new capabilities enable businesses to proactively address challenges and capitalize on opportunities.    

Updates to Transitions in the Power Platform Communities

We're embarking on a journey to enhance your experience by transitioning to a new community platform. Our team has been diligently working to create a fresh community site, leveraging the very Dynamics 365 and Power Platform tools our community advocates for.  We started this journey with transitioning Copilot Studio forums and blogs in June. The move marks the beginning of a new chapter, and we're eager for you to be a part of it. The rest of the Power Platform product sites will be moving over this summer.   Stay tuned for more updates as we get closer to the launch. We can't wait to welcome you to our new community space, designed with you in mind. Let's connect, learn, and grow together.   Here's to new beginnings and endless possibilities!   If you have any questions, observations or concerns throughout this process please go to https://aka.ms/PPCommSupport.   To stay up to date on the latest details of this migration and other important Community updates subscribe to our News and Announcements forums: Copilot Studio, Power Apps, Power Automate, Power Pages

Users online (833)