Solved

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

Posted on 2010-08-26
5
861 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 22

Author Comment

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

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 22

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 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

19 Experts available now in Live!

Get 1:1 Help Now