C#, Move to next item in a collection

Is there a way to "control" when to move to the next item in a collection?

Scenerio: I have 6 files, and 3 directories.
I want to place 2 files in each directory.

It does not look like I could use a nested foreach loop.
Here is a pseudo of what I had in mind.

I'll take pseudo code as answer as well, or other suggestions. Thanks.
foreach(FilesInfo file in myFilesCollection)
{
 
file.CopyTo(currentFolder);
counter++;
 
if(counter == MaxFilesPerFolder)
{
currentFolder = DirectoryInfo.GetNext();
counter = 0;
}
 
 
}

Open in new window

silentthread2kSenior Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

j_s_kelleyCommented:
Do it backwards.
Psuedo:

Try having the outer loop count the directories and then in that loop move files.  When you get to the max file number to move, perform the "continue" command and it will break and move to the next directory.
0
j_s_kelleyCommented:
Actually, Here is how I would do it.  You are currently using a collection of files and a collection of directories.  You can access the members of a collection by index too (not just the foreach).  the pseudo code should look something like this.  The % is modulus division.

int CurDirNdx=0;
Directory curDir = DirectoryList[CurDirNdx];
for(int CurFileNdx=0;CurFileNdx<FileList.Count;CurFileNdx++)
{
  file.CopyTo(curDir);
  if (CurFileNdx % MaxFilesPerdir == 0)
  {
    CurDirNdx++;
    curDir = DirectoryList[CurDirNdx];
  }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.