-
Aug 31st, 2014, 07:18 PM
#1
Thread Starter
Lively Member
Recursive file searching
Hi.
I have some code which recursively searches for files on a folder. The problem is I want it to change a boolean value once it has completed. I don't mean once it gets to the end of the sub, I mean once it's literally finished. I had to code it the way it is otherwise I would have gotten Exception errors and I need it to scan all the files it does (recycling bin etc) because it needs to do behaviour analysis which I have already done. (it already scans, just need that value thing).
The code I have is:
Code:
Dim strFolders() As String = System.IO.Directory.GetDirectories(strPath)
Dim strFiles() As String = System.IO.Directory.GetFiles(strPath, strPattern)
It's initialized like: RecursiveSearch("C:\Users\profilename\Desktop\foldertest\", "*.*", ListBox1)
When all the files are added I need it to change a boolean value. You can't use things to count files as at some points I do it for the whole drive and you'd get a error. The boolean value will represent whether it's finished scanning or not.
Cheers, this forum is really helpful and hopefully someone will be able to help! I've been trying for days. Tried resetting boolean at end of sub etc. By the way, the recursive file search runs on a thread, and yes I have tried making a check if it's alive but it switches when it recalls so that doesn't work.
Last edited by visualBEER310; Sep 14th, 2014 at 12:10 PM.
-
Aug 31st, 2014, 11:15 PM
#2
Fanatic Member
Re: Recursive file searching
What you might try is have a global variable that is used as a counter. Make it a long variable which would handle 9 e+18, which should be plenty big.
Start out with a counter = 1 for the starting folder, and decrement it when you process a folder. Add the count of subfolders to the the counter. At the end of the recursive subroutine, if the counter = 0, you have exhausted all the folders on the drive.
I hope that helps.
-
Aug 31st, 2014, 11:30 PM
#3
Fanatic Member
Re: Recursive file searching
The code that I imagine would be something like the following:
Code:
Class Search
dim ctr as long = 1
dim finishflag as boolean = false
sub RecursiveSearch (strFolder as string, strPattern as string, lstTarget as listbox)
Dim strFolders() As String = System.IO.Directory.GetDirectories(strPath)
Dim strFiles() As String = System.IO.Directory.GetFiles(strPath, strPattern)
cnt = cnt - 1
'Add the files
For Each strFile As String In strFiles
Try
ListBox1.Items.Add(strFile.ToString())
' total = ListBox1.Items.Count
' ListBox1.Refresh()
Catch ex As Exception
End Try
Next
'Look through the other folders
cnt = cnt + strfolders.count
For Each strFolder As String In strFolders
Try
'Call the procedure again to perform the same operation
RecursiveSearch(strFolder, strPattern, lstTarget)
' ListBox1.Refresh()
Catch ex As Exception
End Try
Next
if cnt = 0 then finishflag = true
end sub
-
Sep 1st, 2014, 04:46 AM
#4
Thread Starter
Lively Member
Re: Recursive file searching
Thanks, that was really helpful. I'll try it now and see if it works and report back the results
-
Sep 1st, 2014, 04:58 AM
#5
Thread Starter
Lively Member
Re: Recursive file searching
Next
Catch ex As Exception
processedfiles.Text += 1
Dim creationtime As String = fi.CreationTime
Catch ex As Exception
End Try
watch.Reset()
End If[/CODE]
Last edited by visualBEER310; Sep 14th, 2014 at 12:11 PM.
-
Sep 1st, 2014, 05:19 AM
#6
Thread Starter
Lively Member
Re: Recursive file searching
Originally Posted by John_SC
The code that I imagine would be something like the following:
Code:
Class Search
dim ctr as long = 1
dim finishflag as boolean = false
sub RecursiveSearch (strFolder as string, strPattern as string, lstTarget as listbox)
Dim strFolders() As String = System.IO.Directory.GetDirectories(strPath)
Dim strFiles() As String = System.IO.Directory.GetFiles(strPath, strPattern)
cnt = cnt - 1
'Add the files
For Each strFile As String In strFiles
Try
ListBox1.Items.Add(strFile.ToString())
' total = ListBox1.Items.Count
' ListBox1.Refresh()
Catch ex As Exception
End Try
Next
'Look through the other folders
cnt = cnt + strfolders.count
For Each strFolder As String In strFolders
Try
'Call the procedure again to perform the same operation
RecursiveSearch(strFolder, strPattern, lstTarget)
' ListBox1.Refresh()
Catch ex As Exception
End Try
Next
if cnt = 0 then finishflag = true
end sub
Wait, there seems to be a problem. Basically, the problem is that it seems to only get the files in the first directory (root directory) but it gets them over and over about 30 - 50 times and doesn't add any files in the other subfolders etc. Any ideas?
-
Sep 1st, 2014, 06:32 AM
#7
Re: Recursive file searching
Hello,
Here is a working example for recursively iterating a selected folder and sub-folders for a specific file type and can be modified to look for a file via a full name. Think of this code as a base that can be modified as needed. Lastly the searching works in a backgrounder worker that provides the ability to cancel and report progress.
V2012 project.
-
Sep 1st, 2014, 07:14 AM
#8
Thread Starter
Lively Member
Re: Recursive file searching
Originally Posted by kevininstructor
Hello,
Here is a working example for recursively iterating a selected folder and sub-folders for a specific file type and can be modified to look for a file via a full name. Think of this code as a base that can be modified as needed. Lastly the searching works in a backgrounder worker that provides the ability to cancel and report progress.
V2012 project.
Thank you, I'll look at it now
-
Sep 1st, 2014, 07:40 AM
#9
Re: Recursive file searching
i do not really understand what the problem is. you say
It's initialized like: RecursiveSearch("C:\Users\profilename\Desktop\foldertest\", "*.*", ListBox1)
so its finished once this line of code has executed. no?
John_SC's idea is missing something: it needs to increase the count on the function entry and decrease on function return and only set the flag when after the decrease the counter is 0. so the counter will actually count the recursion depth. this can as well be done using a static var instead of a global var.
-
Sep 1st, 2014, 10:26 AM
#10
Thread Starter
Lively Member
Re: Recursive file searching
Originally Posted by digitalShaman
i do not really understand what the problem is. you say
so its finished once this line of code has executed. no?
John_SC's idea is missing something: it needs to increase the count on the function entry and decrease on function return and only set the flag when after the decrease the counter is 0. so the counter will actually count the recursion depth. this can as well be done using a static var instead of a global var.
No. It calls RecurseSearch inside of the method recursively for each Folder to add the files in the folder. Look at the method and you'll see after files are added in strPath it calls itself and changes strPath to the new folder found in the directory and it will do this for every folder in the root directory set as strPath to start it off.
-
Sep 1st, 2014, 10:31 AM
#11
Re: Recursive file searching
Originally Posted by visualBEER310
No. It calls RecurseSearch inside of the method recursively for each Folder to add the files in the folder. Look at the method and you'll see after files are added in strPath it calls itself and changes strPath to the new folder found in the directory and it will do this for every folder in the root directory set as strPath to start it off.
No, a recursive search of folders will return to the original caller when all folders have been searched. e.g.
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
SearchFolders(startFolder, thePattern, ListBox1)
End Sub
Dim startFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Dim thePattern As String = "*.*"
Private Sub SearchFolders(folder As String, pattern As String, aListBox As ListBox)
Try
Dim strFolders() As String = System.IO.Directory.GetDirectories(folder)
Dim strFiles() As String = System.IO.Directory.GetFiles(folder, pattern)
Me.Invoke(Sub()
aListBox.Items.Add(folder)
End Sub)
Try
For Each f As String In strFiles
Next
For Each d As String In strFolders
SearchFolders(d, pattern, aListBox)
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
'Stop
End Try
Catch unauth As UnauthorizedAccessException
Debug.WriteLine(unauth.Message)
'Stop
Catch ex As Exception
Debug.WriteLine(ex.Message)
'Stop
End Try
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|