yo_bee
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.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.UnauthorizedAccessE xception: Access to the path 'K:\~snapshot\hourly.0\999 144\flhcon f' is denied.
at System.IO.__Error.WinIOErr or(Int32 errorCode, String maybeFullPath)
at System.IO.Directory.Intern alGetFileD irectoryNa mes(String path, String userPathOriginal, String searchPattern, Boolean includeFiles, Boolean includeDirs, SearchOption searchOption)
at System.IO.DirectoryInfo.Ge tFiles(Str ing searchPattern, SearchOption searchOption)
at System.IO.DirectoryInfo.Ge tFiles(Str ing searchPattern)
at FolderBrowser.Form1.Doit(S tring startingDir, StreamWriter sw) in C:\Users\boscam\Documents\ Visual Studio 2008\Projects\FolderBrowse r\FolderBr owser\Form 1.vb:line 58
at FolderBrowser.Form1.Doit(S tring startingDir, StreamWriter sw) in C:\Users\boscam\Documents\ Visual Studio 2008\Projects\FolderBrowse r\FolderBr owser\Form 1.vb:line 84
at FolderBrowser.Form1.Doit(S tring startingDir, StreamWriter sw) in C:\Users\boscam\Documents\ Visual Studio 2008\Projects\FolderBrowse r\FolderBr owser\Form 1.vb:line 84
at FolderBrowser.Form1.Doit(S tring startingDir, StreamWriter sw) in C:\Users\boscam\Documents\ Visual Studio 2008\Projects\FolderBrowse r\FolderBr owser\Form 1.vb:line 84
at FolderBrowser.Form1.Doit(S tring startingDir, StreamWriter sw) in C:\Users\boscam\Documents\ Visual Studio 2008\Projects\FolderBrowse r\FolderBr owser\Form 1.vb:line 84
at FolderBrowser.Form1.RunSea rch() in C:\Users\boscam\Documents\ Visual Studio 2008\Projects\FolderBrowse r\FolderBr owser\Form 1.vb:line 45
at FolderBrowser.Form1.Button 2_Click(Ob ject sender, EventArgs e) in C:\Users\boscam\Documents\ Visual Studio 2008\Projects\FolderBrowse r\FolderBr owser\Form 1.vb:line 26
at System.Windows.Forms.Contr ol.OnClick (EventArgs e)
at System.Windows.Forms.Butto n.OnClick( EventArgs e)
at System.Windows.Forms.Butto n.OnMouseU p(MouseEve ntArgs mevent)
at System.Windows.Forms.Contr ol.WmMouse Up(Message & m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.Butto nBase.WndP roc(Messag e& m)
at System.Windows.Forms.Butto n.WndProc( Message& m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.OnMessa ge(Message & m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.WndProc (Message& m)
at System.Windows.Forms.Nativ eWindow.Ca llback(Int Ptr 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/Microso ft.NET/Fra mework/v2. 0.50727/ms corlib.dll
-------------------------- ---------- ----
FolderBrowser
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/boscam/Do cuments/Vi sual%20Stu dio%202008 /Projects/ FolderBrow ser/Folder Browser/bi n/Release/ FolderBrow ser.exe
-------------------------- ---------- ----
Microsoft.VisualBasic
Assembly Version: 8.0.0.0
Win32 Version: 8.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembl y/GAC_MSIL /Microsoft .VisualBas ic/8.0.0.0 __b03f5f7f 11d50a3a/M icrosoft.V isualBasic .dll
-------------------------- ---------- ----
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembl y/GAC_MSIL /System/2. 0.0.0__b77 a5c561934e 089/System .dll
-------------------------- ---------- ----
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembl y/GAC_MSIL /System.Wi ndows.Form s/2.0.0.0_ _b77a5c561 934e089/Sy stem.Windo ws.Forms.d ll
-------------------------- ---------- ----
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembl y/GAC_MSIL /System.Dr awing/2.0. 0.0__b03f5 f7f11d50a3 a/System.D rawing.dll
-------------------------- ---------- ----
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembl y/GAC_MSIL /System.Co nfiguratio n/2.0.0.0_ _b03f5f7f1 1d50a3a/Sy stem.Confi guration.d ll
-------------------------- ---------- ----
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembl y/GAC_MSIL /System.Xm l/2.0.0.0_ _b77a5c561 934e089/Sy stem.Xml.d ll
-------------------------- ---------- ----
System.Runtime.Remoting
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembl y/GAC_MSIL /System.Ru ntime.Remo ting/2.0.0 .0__b77a5c 561934e089 /System.Ru ntime.Remo ting.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
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.UnauthorizedAccessE
at System.IO.__Error.WinIOErr
at System.IO.Directory.Intern
at System.IO.DirectoryInfo.Ge
at System.IO.DirectoryInfo.Ge
at FolderBrowser.Form1.Doit(S
at FolderBrowser.Form1.Doit(S
at FolderBrowser.Form1.Doit(S
at FolderBrowser.Form1.Doit(S
at FolderBrowser.Form1.Doit(S
at FolderBrowser.Form1.RunSea
at FolderBrowser.Form1.Button
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4952 (win7RTMGDR.050727-4900)
CodeBase: file:///C:/Windows/Microso
--------------------------
FolderBrowser
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/boscam/Do
--------------------------
Microsoft.VisualBasic
Assembly Version: 8.0.0.0
Win32 Version: 8.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembl
--------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembl
--------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembl
--------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembl
--------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembl
--------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembl
--------------------------
System.Runtime.Remoting
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembl
--------------------------
************** 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
ASKER