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 files based on fi...
Power Automate
Unanswered

Move files based on file names

(0) ShareShare
ReportReport
Posted on by 4

Hi guys,

 

I am new to Power Automate and with no IT background, and recently know it exists and really hope to use it in my day-to-day work to make my work more efficient.

 

I think this question should has been asked by others before. But I could get the logic behind its and how's work with Power Automate. 

 

My case is here:

1. I have many source files (e.g. Jpeg or PDF) named according to certain prefix index, like 1 to 99 with suffix a to d (e.g. 1_a.jpeg, 1_b.jpeg, 1_c.jpeg......99_c_jpeg);

2. Then I have a destination folder containing sub-folders named as the prefix index (In this case the prefix index is 1 to 99, so that is 99 sub-folders in the destination folder);

3. I want to relocate (or copy) the source files to the sub-folders in the destination according to the index. Such that each sub-folder should contains 4 source file (e.g. For index = 55, then it contains four PDf files named as 55_a, 55_b, 55_c and 55_d). 

 

Any one could help? Many thanks!

 

 

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    For #1:

    Get Files 'this will list all your files to the variable %Files%

     

    For #2 & 3:

    Loop 0 to %Files.Count - 1% increment of 1 'what loop you are on is identified as %LoopIndex%

        Split Text %Files[LoopIndex].NameWithoutExtension% by Custom delimiter _ (an underscore) to %TextList%

        'The %Files[LoopIndex].NameWithoutExtension% essentially turn 99_a.jpg to 99_a

        'The Split Text on underscore will turn 99_a into a List where index 0 is 99 and index 1 is a

        Set variable %CurrentFolder% to %TextList[0]%

        Move files %Files[LoopIndex]% to [Master Path ex. C:\SavedFolders]\%CurrentFolder%

        'looks like you want to convert them all to PDF also, but that is harder to do.  You have to set up PDF as your default printer, print, and save them, so that is another topic for another day.  Let's get them all into the right folder first, and then we will worry about converting them to PDF.

    End (Loop)

     

    Good luck!

  • Henrik_M Profile Picture
    2,021 Super User 2024 Season 1 on at

    Get files in folder (your main folder)

    LOOP - For each (of %Files%)

            Split text (of %CurrentItem% with underscore_character) %TextList[0]% now contains the prefix number

            Copy file (%CurrentItem%) --> mainfolder\%TextList[0]%

    LOOP END

     

    That should work.

  • BrianTH107 Profile Picture
    4 on at

    Hi Michael,

     

    Thanks for your reply.

     

    The "Get Files" and "Split Text" seems successful. But looped split text seems only split 99_d to 99 as 0 and d as 1, but the files in between 1_a to 99_c is not stored in the %TextList% .  

     

    I think I may misunderstand the loop function, below is my input

    BrianTH107_0-1643215849746.png

     

    As as result, the flow only move 99_d.jpeg to the new destination (not even reach the sub-folder in C:\Destination\. Is it the script above does not include putting those files into the corresponding sub-folder according to the prefix index.

  • BrianTH107 Profile Picture
    4 on at

    I changed the "Start from" input from %Files.Count - 1% to 0, then it works! it put all the files from 1_a to 99_d to the destination folder 1 to folder 99, which each folder contains 4 files _a to _d.

     

    It helps a lot! 

     

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    Nope...start from 0 end at %Files.Count - 1%

     

    Files is a 0 based index, so %Files[0]% is the first file, %Files[1]% is the second.  So we are saying, start from 0 to the number of files minus 1.  Then %LoopIndex% always refers to our current number we need.

     

    Henrik's way works too, I just prefer to use Loops, because I always know the number that I'm working on, and if it fails, where I left off.

     

    Good luck!

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    Glad to hear.  As far as making them all PDFs, I would walk through Print to PDF procedures and then move the old file out to another folder.  I wouldn’t delete just in case something goes wrong. 

  • Arunrao Profile Picture
    2 on at

    Copy my file based on Excel file data Please tell me how ? in power automate desktop

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

Leaderboard > Power Automate

#1
11manish Profile Picture

11manish 243 Super User 2026 Season 2

#2
David_MA Profile Picture

David_MA 211 Super User 2026 Season 2

#3
Mohsin Ali Profile Picture

Mohsin Ali 190

Last 30 days Overall leaderboard