Link to home
Start Free TrialLog in
Avatar of yo_bee
yo_beeFlag for United States of America

asked on

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.

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jini Jose
Jini Jose
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yo_bee

ASKER

What does this do?
Avatar of yo_bee

ASKER

Like this?
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
        Application.DoEvents()

        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

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yo_bee

ASKER

Just got this Access Denied error.
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.UnauthorizedAccessException: Access to the path 'K:\~snapshot\hourly.0\999144\flhconf' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.Directory.InternalGetFileDirectoryNames(String path, String userPathOriginal, String searchPattern, Boolean includeFiles, Boolean includeDirs, SearchOption searchOption)
   at System.IO.DirectoryInfo.GetFiles(String searchPattern, SearchOption searchOption)
   at System.IO.DirectoryInfo.GetFiles(String searchPattern)
   at FolderBrowser.Form1.Doit(String startingDir, StreamWriter sw) in C:\Users\boscam\Documents\Visual Studio 2008\Projects\FolderBrowser\FolderBrowser\Form1.vb:line 58
   at FolderBrowser.Form1.Doit(String startingDir, StreamWriter sw) in C:\Users\boscam\Documents\Visual Studio 2008\Projects\FolderBrowser\FolderBrowser\Form1.vb:line 84
   at FolderBrowser.Form1.Doit(String startingDir, StreamWriter sw) in C:\Users\boscam\Documents\Visual Studio 2008\Projects\FolderBrowser\FolderBrowser\Form1.vb:line 84
   at FolderBrowser.Form1.Doit(String startingDir, StreamWriter sw) in C:\Users\boscam\Documents\Visual Studio 2008\Projects\FolderBrowser\FolderBrowser\Form1.vb:line 84
   at FolderBrowser.Form1.Doit(String startingDir, StreamWriter sw) in C:\Users\boscam\Documents\Visual Studio 2008\Projects\FolderBrowser\FolderBrowser\Form1.vb:line 84
   at FolderBrowser.Form1.RunSearch() in C:\Users\boscam\Documents\Visual Studio 2008\Projects\FolderBrowser\FolderBrowser\Form1.vb:line 45
   at FolderBrowser.Form1.Button2_Click(Object sender, EventArgs e) in C:\Users\boscam\Documents\Visual Studio 2008\Projects\FolderBrowser\FolderBrowser\Form1.vb:line 26
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4952 (win7RTMGDR.050727-4900)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
FolderBrowser
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/boscam/Documents/Visual%20Studio%202008/Projects/FolderBrowser/FolderBrowser/bin/Release/FolderBrowser.exe
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 8.0.0.0
    Win32 Version: 8.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Runtime.Remoting
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
AccessDeniedError.JPG