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)
if(counter == MaxFilesPerFolder)
currentFolder = DirectoryInfo.GetNext();
counter = 0;

Open in new window

silentthread2kSenior Software EngineerAsked:
Who is Participating?
j_s_kelleyConnect With a Mentor Commented:
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++)
  if (CurFileNdx % MaxFilesPerdir == 0)
    curDir = DirectoryList[CurDirNdx];
Do it backwards.

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.