Directory search program duplicating output

This code is listing the directories in a given drive.  Why is it duplicating output?

   Private Sub ScanDrive(DriveToScan As DriveInfo)

        Dim path As String = DriveToScan.ToString  '"c:\\"
        Dim searchPattern As String = "*"
        Using sw As New StreamWriter(DirListFile, False)
            Try
                Dim di As DirectoryInfo = New DirectoryInfo(path)
                Dim directories() As DirectoryInfo = di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly)

                Dim files() As FileInfo = di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly)
                Dim dir As DirectoryInfo
                Dim DirSub As DirectoryInfo
                Dim DirSub2 As DirectoryInfo
                Dim DirSub3 As DirectoryInfo
                Dim DirSub4 As DirectoryInfo
                Dim DirSub5 As DirectoryInfo
                sw.WriteLine("Directory")
                If DriveToScan.IsReady = True Then
                    Try
                        di = New DirectoryInfo(DriveToScan.ToString)
                        directories = di.GetDirectories("*", SearchOption.AllDirectories) 'searchPattern, SearchOption.TopDirectoryOnly)

                        files = di.GetFiles(searchPattern, SearchOption.AllDirectories)
                    Catch ex As Exception
                        If ex.Message.Contains("Access to the path") Then 'carry on
                        Else
                            MsgBox("Page Error.  " & ex.Message + ". Line = " & Convert.ToInt32(ex.StackTrace.Substring(ex.StackTrace.LastIndexOf(" "c))))
                            End
                        End If

                    End Try
                    For Each dir In directories
                        If dir.FullName.Contains("Windows") Or dir.FullName.Contains("Program Files") Then
                        Else

                            'If Not ((dir.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden) Then
                            sw.WriteLine(dir.FullName) ' & "," & Round((DirectorySize(dir, True) / 1048576), 3).ToString)
                            Try
                                For Each DirSub In dir.GetDirectories
                                    'Using sw As New StreamWriter(DirListFile, True)
                                    If DirSub.GetDirectories.Count = 0 Then
                                        sw.WriteLine(DirSub.FullName) ' + "," + Round((DirectorySize(DirSub, True) / 1048576), 3).ToString)
                                    End If
                                    For Each DirSub2 In DirSub.GetDirectories
                                        If DirSub2.GetDirectories.Count = 0 Then
                                            sw.WriteLine(DirSub2.FullName) ' + "," + Round((DirectorySize(DirSub2, True) / 1048576), 3).ToString)
                                        End If
                                        For Each DirSub3 In DirSub2.GetDirectories
                                            If DirSub3.GetDirectories.Count = 0 Then
                                                sw.WriteLine(DirSub3.FullName) ' + "," + Round((DirectorySize(DirSub3, True) / 1048576), 3).ToString)
                                            End If
                                            For Each DirSub4 In DirSub3.GetDirectories
                                                If DirSub4.GetDirectories.Count = 0 Then
                                                    sw.WriteLine(DirSub4.FullName) ' + "," + Round((DirectorySize(DirSub4, True) / 1048576), 3).ToString)
                                                End If
                                                For Each DirSub5 In DirSub4.GetDirectories
                                                    If DirSub5.GetDirectories.Count = 0 Then
                                                        sw.WriteLine(DirSub5.FullName) '+ "," + Round((DirectorySize(DirSub5, True) / 1048576), 3).ToString)
                                                    End If
                                                Next
                                            Next
                                        Next
                                    Next
                                    'End Using
                                Next
                            Catch UnAuthDir As UnauthorizedAccessException
                            Catch ex As Exception
                                If ex.Message.Contains("Access to the path") Then 'carry on
                                    'MsgBox("Scan Error 1.  " & ex.Message + ". Line = " & Convert.ToInt32(ex.StackTrace.Substring(ex.StackTrace.LastIndexOf(" "c))))
                                    'sw.WriteLine(dir.FullName + "," + dir.LastWriteTime + "," + "0")
                                    'End Using
                                Else
                                    MsgBox("Scan Error 1.  " & ex.Message + ". Line = " & Convert.ToInt32(ex.StackTrace.Substring(ex.StackTrace.LastIndexOf(" "c))))
                                    End
                                End If
                            End Try
                            'End Using
                            'Next
                        End If

                    Next dir

                End If
            Catch UnAuthDir As UnauthorizedAccessException
                MsgBox("Scan Error 2.  " & UnAuthDir.Message) ' + ". Line = " & Convert.ToInt32(UnAuthDir.StackTrace.Substring(UnAuthDir.StackTrace.LastIndexOf(" "c))))
            Catch ex As Exception
                MsgBox("Scan Error 2.  " & ex.Message) ' + ". Line = " & Convert.ToInt32(ex.StackTrace.Substring(ex.StackTrace.LastIndexOf(" "c))))
            End Try
        End Using
    End Sub

Open in new window

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

Bill PrewIT / Software Engineering ConsultantCommented:
It looks like you find all directories here:

directories = di.GetDirectories("*", SearchOption.AllDirectories)

but then below that you do nested FOR loops exploding each found directory.  This seems like it could produce duplicates.

What are you trying to accomplish?


»bp
AlHal2Author Commented:
Thanks.  At each run the program lists the directories then compares that list with previous run to see if any directories have been renamed or deleted.
Bill PrewIT / Software Engineering ConsultantCommented:
Then I would suggest changing this line:

   directories = di.GetDirectories("*", SearchOption.AllDirectories)

to:

   directories = di.GetDirectories("*", SearchOption.TopDirectoryOnly)

I also don't see the files variable referenced, so removing these lines could save some execution time...

   files = di.GetFiles(searchPattern, SearchOption.AllDirectories)


»bp

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
aranaCommented:
I have run your code and i have no duplicates, what are you passing as a parameter?
AlHal2Author Commented:
thanks
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
Visual Basic.NET

From novice to tech pro — start learning today.