cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
CodyJennings
Frequent Visitor

Looping through files multiple times

Flow intentions:

Recurrence once per week

Get all files in folder (currently 22 files)

Loop through each file (each file contains an array of up to 5000 items)

Create a new file with either 150 items or up to the last item if the end of the file is reached

 

Problems:

I am seeing that all files are placed in the correct folder with a max of 150 items as I expect. Even after the flow finishes with the 22 files, it continues and starts looping through the first file again. If it would end when I expect it to, it would take roughly 20 hours to complete.

 

Flow:

CodyJennings_0-1636749557132.png

 

CodyJennings_1-1636749575148.png

 

CodyJennings_2-1636749592438.png

 

CodyJennings_3-1636749627616.png

 

Is there a better way to set this up? Why is it restarting the loop when it finishes with the last file? Can this process be ran in parallel to speed up the amount of time it takes?

1 REPLY 1
Gopala_Krishna
Super User
Super User

@CodyJennings 

Your flow is going into continuous loops because of the "Apply to each".

 

When you use  "Apply to each" action it takes an array as input and loops through each item in the array. If my array has 5 items the loop will run 5 items and like wise.

 

In your scenario I think the problem is that in the first or second iteration of the loop all the files are getting created, the next iterations are happening because of the number of elements in the array.

 

To break this you can make use of the terminate action in Power Automate, this will help you in stopping the flow execution 

 

 

Gopala_Krishna_0-1636806111237.png

 

You can also try creating parallel branches in the flow, but I don't think that would have a major impact considering that the actions are nested in an Apply to Each loop. I would suggest use of Terminate action.

 

If the information shared helps you please consider giving a thumbs up 👍 and mark solution as resolved

Please follow my website PowerCards for more information related to Power Platform

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Users online (2,477)