Question : Form seems like it is hang during Directory.Getfiles on a large directory structure.

I have a form that I built that recurisavly goes through a directory structure.  This for works, but states that it is not responding when dealing with a very large directory structure.  I would like to have some sort of progress bar and display the most current directory/file being looked at.

My form consists of:
3 textboxes (1 for the folder path, 1 for the age of lastwriten, 1 for the file pattern)
2 buttons (1 for folder browsing and the other to start the search)

My code might or is pourly writen so that might be the reason.  Attached is the code to help shed some light on this.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
Imports System.IO

Public Class Form1
   

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
            TextBox1.Text = FolderBrowserDialog1.SelectedPath

        End If

    End Sub

    Dim f, d As Integer

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim StrFiles As String
        StrFiles = TextBox1.Text
        SaveFileDialog1.Title = "Please Select a File"
        SaveFileDialog1.InitialDirectory = "C:\Users\boscam\Documents"
        SaveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        SaveFileDialog1.ShowDialog()


        Using sw As StreamWriter = New StreamWriter(SaveFileDialog1.FileName) ', False, System.Text.Encoding.Unicode)
            sw.WriteLine("Directory Path" & vbTab & "File Name" & vbTab & "Date Accessed")
            f = 0
            d = 0
            sw.WriteLine(StrFiles)

            Doit(StrFiles, sw)
        End Using
        RichTextBox1.Text = ("Search has completed and outputed to: " & SaveFileDialog1.FileName & vbCrLf _
                             & "Number of Directories searched: " & d & vbCrLf _
                             & "Number of Files searched: " & f)


    End Sub

    Private Sub Doit(ByVal startingDir As String, ByVal sw As StreamWriter)
        Dim StartDI As New DirectoryInfo(startingDir)
        Dim SubFI As FileInfo() = StartDI.GetFiles(TextBox3.Text)
        Dim SubDI As DirectoryInfo() = StartDI.GetDirectories
        'Dim S As Long = 0
        ' do files first
        For Each SubFItemp In SubFI
            If DateDiff(DateInterval.Day, SubFItemp.LastWriteTime, Now) > TextBox2.Text Then
                'MsgBox(DateDiff(DateInterval.Day, SubFItemp.LastWriteTime, Now))
                sw.WriteLine(vbTab & SubFItemp.Name & vbTab & SubFItemp.LastWriteTime & vbTab & (DateDiff(DateInterval.Day, SubFItemp.LastAccessTime, Now)))
                'SubFItemp.Delete()
                f = f + 1
            End If
        Next
        ' do the subdirectories, and then descend into the directory structure
        For Each SubDITemp In SubDI
            sw.WriteLine(SubDITemp.FullName & vbTab & vbTab & vbTab & d)
            d = d + 1
            ' a recursive call to this routine
            Doit(SubDITemp.FullName, sw)
        Next

    End Sub

End Class

Answer : Form seems like it is hang during Directory.Getfiles on a large directory structure.

add this line inside your DoIt function

Application.DoEvents()
Random Solutions  
 
programming4us programming4us