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 / Sorting files into var...
Power Automate
Unanswered

Sorting files into various folders based on an Product Code

(0) ShareShare
ReportReport
Posted on by 6

Hi there, 

 

Love Power Automate Desktop and find it super powerful, but have trouble building flows on my own.  This time is no exclusion.

 

I have a number of Products which have 4 (or so) images in a folder. 

Each photo is labelled eg>MZWOK_01, MZWOK_02 etc.  Example below.

Robert_Jeffs_0-1659859074343.png

I'm trying to find a way to take the Product Code (MZWOK) and move all the associated MZWOK files to that directory.  And doing this in a loop to capture all the products and files.  I have a list of the product codes at hand so can reference that if needed, I've also run a macro through excel to create all the folders, i just to move the files into their corresponding folder!  below is the beginnings of the flow I started, but got a bit lost...

Robert_Jeffs_1-1659859291664.png

 

thanks for your help!

I have the same question (0)
  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    Instead of read from excel A2, first, “get first free row” on column A, the read A2 to A and %FirstFreeRow - 1%.  Also, do this first, and label it %Folders%…as I understand it, this will be a list of your product codes. Each file starts with this product code and each file that starts with this product code needs to move to a folder with the same product code.

     

    Next, define the %FromPath% and %ToPath% using set variable. The %FromPath% is where all the images currently reside. The %ToPath% is where all the product folders reside you want to file to.

     

    Now, we simply loop on the product list to “Move files” that start with each product code to the corresponding folder.

     

    For each %Folder% in %Folders%
        If file exists %FromPath%\%Folder%*
            Move files %FromPath%\%Folder%* to %ToPath%\%Folder%
        End (IF)

    End (Loop)

     

    It should loop through each product code and if it finds any files, move them to the new folder. Best of luck!

  • Robert_Jeffs Profile Picture
    6 on at

    Hi @MichaelAnnis 

    Thanks very much for your response, appreciate it!

     

    A couple of questions:

     

    1:

    I can see two variables that are very similar to each other %Folder% and %Folders% although I can't see where the first one is created?

     

    2:

    Are %ToPath% and %NewPath% the same?  I can't see where %ToPath% is utilised in the loop.

     

    Thank you again for your help, i really appreciate it!!

  • ryule Profile Picture
    929 Super User 2024 Season 1 on at

    Could you loop through a list of product codes, and in the loop, have your "Get Files" Action file filter look like this? And use that list to move the files...

     

    ryule_0-1659969987429.png

     

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    Sorry, yes, %ToPath% and %NewPath% was an error...I changed it later and missed a correction...I will edit the post.  %Folders% is made from "Read excel A2 to A and %FirstFreeRow - 1%.

     

    %Folder% is meant to be a single item from the list %Folders%.  By default, if you run "For Each" on %Folders%, it will default to %CurrentItem%...my recommendation was to change %CurrentItem% to %Folder%

  • Robert_Jeffs Profile Picture
    6 on at

    HI @MichaelAnnis 

     

    I'm making progress, and i again appreciate your help.  I believe I've put together everything per your suggestion, but still am having trouble.  I've moved everything to a test area contained in the one spot on in a file structure on my desktop.  

     

    Robert_Jeffs_0-1660030599436.png

    I've grabbed a bunch of images and renamed them as per below:

    Robert_Jeffs_1-1660030644147.png

    in my xls, i've done the same:

    Robert_Jeffs_2-1660030679634.png

     

    the flow runs, but does nothing, images are still in the Images_All area after running.  Below is the flow after having embraced your suggestions to letter (at least i hope to the letter!)

     

    Robert_Jeffs_4-1660030919652.png

    any idea where i might be going wrong?  thanks again for your help, i really very much appreciate it.

     

    thanks again

     

     

  • ryule Profile Picture
    929 Super User 2024 Season 1 on at

    I believe before your "Move file(s)" action, you need a get files action from your FromPath\Folder, and then use that output for the Move Files action

  • Pavel_NaNoi Profile Picture
    1,074 on at

    wildcards "*" do not work in IF statements, that's why nothing is happening, your IF statement is never gone inside of as its looking literally for "A*" but there is no file labelled "A*" literally. I recommend removing it, should work then.

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    Thank you @Pavel_NaNoi,  if that is the case, then do as ryule suggests.  Get Files from %FromPath%\%Folder%*.

     

    Then, If %Files.Count%>0, Move %Files% to %NewPath%/%Folder%

     

     

    Note...get files may actually error out on 0 as in "no files found."  If that is the case, then I would add a LABEL (SKIPLOOP) just short of End (Loop) and put an OnError surrounding the Get Files action to jump to SKIPLOOP (therefore skipping Move Files) since there are no files with this product code.

     

    Sorry for the confusion, sometimes * works, sometimes it doesn't, and I am not always at my computer to test these.

  • Robert_Jeffs Profile Picture
    6 on at

    Hi there @MichaelAnnis@Pavel_NaNoi and @ryule 

    Once again, I appreciate all your help.

     

    With the exception of the Label (Skiploop), I've added all the actions that were suggested, and I believe in the correct spot, and got the result below:

    Robert_Jeffs_2-1660202816399.png

     

    So the result of this loop moves everything to the ...\Images_Sorted\A folder and then errors out.

     

    Have I put 'Get files in folder' in the correct spot?

     

    Again, I really appreciate your help, I very much appreciate it!

     

    HELP!!

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    Did this happen on the first loop or somewhere in the middle of the run?

    Can you show us the variable %Files% when it fails?

     

    Thx,  Mike

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 Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Vish WR Profile Picture

Vish WR 382

#2
Valantis Profile Picture

Valantis 370

#3
David_MA Profile Picture

David_MA 300 Super User 2026 Season 1

Last 30 days Overall leaderboard