?
Solved

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

Posted on 2010-08-26
5
Medium Priority
?
907 Views
Last Modified: 2013-12-26
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

0
Comment
Question by:yo_bee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 10

Accepted Solution

by:
Jini Jose earned 1000 total points
ID: 33535109
add this line inside your DoIt function

Application.DoEvents()
0
 
LVL 23

Author Comment

by:yo_bee
ID: 33535457
What does this do?
0
 
LVL 23

Author Comment

by:yo_bee
ID: 33535463
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

0
 
LVL 17

Assisted Solution

by:Zhaolai
Zhaolai earned 1000 total points
ID: 33535708
!. Add a StatusStrip to your form,
 2. Click on its Items collection, add a StatusLabel.
3. Code below:

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
        StatusStrip1.Items(0).Text = "Processing " & startingDir
        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

0
 
LVL 23

Author Comment

by:yo_bee
ID: 33540907
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question