Solved

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

Posted on 2010-08-26
5
854 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
  • 3
5 Comments
 
LVL 10

Accepted Solution

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

Application.DoEvents()
0
 
LVL 21

Author Comment

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

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 250 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 21

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now