Loop is endless

Hi,
I have 3 levels of folders like
                    string[] dirlvl2Ent = Directory.GetDirectories(dir, "*", SearchOption.AllDirectories);
                    //string[] dirlvl2Ent=System.IO.Directory.GetFiles(dir,"*.*",System.IO.SearchOption.AllDirectories);
                    foreach (string dirlvl2 in dirlvl2Ent)
                    {
                        string[] dirlvl3Ent = Directory.GetDirectories(dir, "*", SearchOption.AllDirectories);
                        //string[] dirlvl2Ent=System.IO.Directory.GetFiles(dir,"*.*",System.IO.SearchOption.AllDirectories);
                        foreach (string dirlvl3 in dirlvl3Ent)
                        {

                            string[] files = Directory.GetFiles(dirlvl3, "*.out");
                            foreach (string f in files)
                            {
                                str.Clear();
                                flnm = f;
                                ...

Open in new window

it seems it does repeatedly loop there endlessly with the same set of sub-folders. why?
LVL 11
HuaMin ChenSystem AnalystAsked:
Who is Participating?
 
Éric MoreauSenior .Net ConsultantCommented:
shouldn't
string[] dirlvl3Ent = Directory.GetDirectories(dir, "*", SearchOption.AllDirectories);

Open in new window


be replaced with
string[] dirlvl3Ent = Directory.GetDirectories(dirlvl2 , "*", SearchOption.AllDirectories);

Open in new window

0
 
AndyAinscowFreelance programmer / ConsultantCommented:
You really ought to learn some simple debugging.  It would save you so much time.

Put a breakpoint on your line 3 and see what the first directory found is - then it will be clear why your code does not work.  (After you fix that then check the second directory before you ask the next question).
0
 
dbruntonCommented:
Or rather than use a debugger put a printf statement to print the result of each foreach result.

I suspect (without looking at your code) that you might be passing the directories . and .. down to the next loop.  If you find those two directories then ignore them.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
sarabandeCommented:
it seems it does repeatedly loop there endlessly with the same set of sub-folders
the nested loop is not infinite but because of the bug Eric has detected, you would have the same set of subfolders for each subfolder of the top folder. that means if the top folder has 10 sub folders the nested loop would search 10 times all those 10 folders.

dbrunton detected the next flaw. you have to skip folder "." and folder ".." for both foreach loops:

foreach (string dirlvl2 in dirlvl2Ent)
{
      if (dirlvl2 == "." || dirlvl2 == "..")
              continue;
      ....
      string[] dirlvl3Ent = Directory.GetDirectories(dirlvl2 , "*", SearchOption.AllDirectories);
      foreach (string dirlvl3 in dirlvl3Ent)
      {
           if (dirlvl3 == "." || dirlvl3 == "..")
                continue;
           ...

Open in new window


please take into account, that the bugs were detected by dbrunton and Eric Moreau, not by me.

Sara
0
 
käµfm³d 👽Commented:
You specified the SearchOption.AllDirectories option to GetDirectories. This means that GetDirectories will recurse all directories under the parent. So why do you have that inner foreach loops? It should be unnecessary.

As to GetDirectories returning "." and "..", this does not happen--at least not in C#. C++ (.NET) may behave differently. GetDirectories returns full paths to folders found under the target folder, not simply folder names.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
@kaufmed.
. and .. are returned in unmanaged C++ when performing a GetDirectories.  Also the unmanaged version doesn't recurse into directories - which is probably why there is this attempt to recurse into the sub-directories.


ps.  Don't forget the debugger is your friend.  At least learn to use some basic functionality.
0
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.