0

There are so many examples for traverse files through folder using foreach loop container but i want to know how to traverse files using For loop container please help me

4
  • What is the motivation for wanting to use the for loop? Commented Apr 20, 2016 at 14:28
  • 1
    Welcome to Stack Overflow. Please see How to Ask for information about how to get the best response from your questions. For example, you should add what you have tried to do specifically and what trouble you have had with what you tried to do. Otherwise, your question is too broad in scope for this site. Commented Apr 20, 2016 at 14:35
  • @Kateract +1 for a polite bit of advice to a new user Commented Apr 20, 2016 at 15:11
  • why do you want to use the for loop? In case you want to ignore some files this is more easily done by an expression task with a condition on a precedence constraint. Commented Apr 24, 2016 at 10:11

1 Answer 1

1

A For loop uses a range and a counter. If you wanted to use a For loop to traverse files in a folder, you would have to use a script task to count the number of files in the folder, and populate a variable with that number.

Then you would have to set a counter variable to 0, and set the For Loop to run while the counter is less than the file count, and increment the counter by 1 for each iteration.

Inside your loop you would need to get the file by its Index within the folder, presumably using the FileSystemObject in a Script Task.

Not particularly efficient, which is why the ForEach loop is usually used for files.

Sign up to request clarification or add additional context in comments.

4 Comments

AFAIK a for loop just loops while the evaluation expression is true. It doesn't have to increment anything.
Well, that's correct, but if you don't increment the counter in this case, the for loop will run infinitely. Am I missing something?
Just making the point that you don't need to count them at all upfront and use an index to loop through them. It could effectively be while(@MoreFilesToProcess) and that could be determined a number of ways. Eg. By the folder not being empty if you are archiving them elsewhere.
Well that's true, if there are other conditions that can be tested to determine when to stop looping, then that eliminates the need for a counter. In the absence of details, I choose as generic an approach as I could think of.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.