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

  • What is the motivation for wanting to use the for loop? – Martin Smith Apr 20 '16 at 14:28
  • 1
    Welcome to Stack Overflow. Please see [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. – Kateract Apr 20 '16 at 14:35
  • @Kateract +1 for a polite bit of advice to a new user – BIDeveloper Apr 20 '16 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. – Felix Apr 24 '16 at 10:11

1 Answers1

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.

Tab Alleman
  • 31,483
  • 7
  • 36
  • 52
  • AFAIK a for loop just loops while the evaluation expression is true. It doesn't have to increment anything. – Martin Smith Apr 20 '16 at 15:10
  • 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? – Tab Alleman Apr 20 '16 at 15:30
  • 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. – Martin Smith Apr 20 '16 at 15:34
  • 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. – Tab Alleman Apr 20 '16 at 15:38