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 12
HuaMin ChenProblem resolverAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

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
dbruntonQuid, Me Anxius Sum?  Illegitimi non carborundum.Commented:
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
É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

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
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

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
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.