AlHal2
asked on
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
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I have run your code and i have no duplicates, what are you passing as a parameter?
ASKER
thanks
directories = di.GetDirectories("*", SearchOption.AllDirectorie
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