Recursive file searching

visualBEER310

Member
Joined
Aug 31, 2014
Messages
6
Programming Experience
1-3
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)  
        '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  
        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

It's initialized like: RecursiveSearch("C:\Users\profilename\Desktop\somefolder\", "*.*", 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.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,171
Location
Sydney, Australia
Programming Experience
10+
Any examples of this? Thanks :)
An example of what? Writing a method? Calling a method? You've already posted examples of both yourself. Setting a Boolean variable? I'm sure that there are lots of examples of that around.
 

visualBEER310

Member
Joined
Aug 31, 2014
Messages
6
Programming Experience
1-3
An example of what? Writing a method? Calling a method? You've already posted examples of both yourself. Setting a Boolean variable? I'm sure that there are lots of examples of that around.
The code is being worked on a thread in the background. At the same time another thread is "processing" information about the files added into the listbox. After that has finished I check if a boolean is finished or not to see if I need to use GoTo to the top to go down the missed items, if it is true then files are loaded and i can display a message to the user. In that code I posted, it recursively calls itself t get the files in the folders and setting a boolean value at the end of it does not work because it loops through (well I have already tried it so....)...

If it is so simple to do whatever your talking about (well I understand what you mean but it does not work with my code) then why don't you show me a example so I fully understand what you mean and can go and modify the example or somewhat to make the code I already have work? Sorry if I sound a bit agitated but I have been trying to get this to work for the past week with no luck. Yeah, it scans right etc but doesn't work when I try to make the other thread aware of when it is complete (let's just say a empty thread *leaving a gap for anyone to step in and help me there*).

Thanks.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,171
Location
Sydney, Australia
Programming Experience
10+
The reason I haven't provided an example is that it should be obvious from the explanation. Instead of calling method A, write method B and call that instead. Inside method B, call method A and then set the the Boolean. It's all but impossible to write an "example" of something that simple without actually writing the code but here goes. You are currently doing this:
SomeMethod()
Instead, write this:
Private Sub SomeOtherMethod()
    SomeMethod()
    someBoolean = True
End Sub
and then do this:
SomeOtherMethod()
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,331
Location
Norway
Programming Experience
10+
Sometimes "calling a method and set a boolean" is just that simple :)
RecursiveMethod(argument)
isComplete = True

The recursive work is completed when the initial call to that method returns.
 

visualBEER310

Member
Joined
Aug 31, 2014
Messages
6
Programming Experience
1-3
Sometimes "calling a method and set a boolean" is just that simple :)
RecursiveMethod(argument)
isComplete = True

The recursive work is completed when the initial call to that method returns.
Yes, but this won't work when it's being ran with threads in the background... At the start it starts on a thread, then another thread is initialized. The boolean would set to True straight away when it's on threads. And it has to be on threads to work quickly...
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,331
Location
Norway
Programming Experience
10+
Then you have to do as jmcilhinney suggested, encapsulate that within the method that is called.
Sub Encapsulate(argument) '(called async)
    RecursiveMethod(argument)
    isComplete = True
End Sub
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,171
Location
Sydney, Australia
Programming Experience
10+
Yes, but this won't work when it's being ran with threads in the background... At the start it starts on a thread, then another thread is initialized. The boolean would set to True straight away when it's on threads. And it has to be on threads to work quickly...
No it won't! Not unless you're actually starting a new thread inside the RecursiveSearch method and you've never said that that's the case and your code doesn't indicate that that the case. If you are starting a thread and then calling RecursiveSearch on that thread then there's no issue. All you have to do is follow my simple instructions. Wherever you are now calling RecursiveSearch you simply call this other method and in that method you call RecursiveSearch and then set the Boolean.

Now, if you actually ARE starting another thread inside the RecursiveSearch method then I have to wonder why you've never actually stated that or shown us the relevant code. Maybe you should just show us the code anyway, so that we can see for ourself either way. Show us the entire RecursiveSearch method and the entire method that calls it. Either you have been wasting time by denying something will work when you could have just done and seen that it works for yourself or you've been wasting time by not providing all the relevant information.
 

visualBEER310

Member
Joined
Aug 31, 2014
Messages
6
Programming Experience
1-3
No it won't! Not unless you're actually starting a new thread inside the RecursiveSearch method and you've never said that that's the case and your code doesn't indicate that that the case. If you are starting a thread and then calling RecursiveSearch on that thread then there's no issue. All you have to do is follow my simple instructions. Wherever you are now calling RecursiveSearch you simply call this other method and in that method you call RecursiveSearch and then set the Boolean.

Now, if you actually ARE starting another thread inside the RecursiveSearch method then I have to wonder why you've never actually stated that or shown us the relevant code. Maybe you should just show us the code anyway, so that we can see for ourself either way. Show us the entire RecursiveSearch method and the entire method that calls it. Either you have been wasting time by denying something will work when you could have just done and seen that it works for yourself or you've been wasting time by not providing all the relevant information.
The code that initializes it is:

Code:
        CheckForIllegalCrossThreadCalls = False

        getDir = New Thread(Sub() Me.RecursiveSearch("C:\", "*.*", ListBox1))
        getDir.IsBackground = True
        getDir.Start()


        scannerthread = New Thread(AddressOf filescanner)
        scannerthread.IsBackground = True
        scannerthread.Start()

^ That is in the form load event (as I test it, and when everything is solved I can make it on a button click).

It must be threaded or it will take far too long.
Then the other thread (scannerthread on the filescanner method) loops through all the files in the listbox whilst they are being added (it adds the selected index so it goes down to the end and not scan the same files). The problem is when the loop is finished (all files have been scanned) it needs to know whether the first thread, getDir(recursivesearch....) is active so it can use the GoTo to restart the looping on the next index or to just display a messagebox like "Completed" and then do whatever. The code for looping etc in the scannerthread method is here:

Code:
Private Sub filescanner()

initializeScan:
        GoTo scannerp


scannerp:
        Dim si As Integer
        For si = 0 To ListBox1.Items.Count - 1
            Try
                ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
            Catch ex As Exception
            End Try
            Try
' do whatever here
            Catch ex As Exception
            End Try
' here is where I need to check if the all files are added in, so i can use the if statement with the boolean to use GoTo like below or to let it pass with the finished messagebox etc.
            GoTo scannerp
        Next si
        MsgBox("Scan finished")
    End Sub

Problem is you will never get to the MsgBox as it loops with GoTo. Would you have a working example to get it with the method suggestion with this code?

Thanks.
 
Last edited:

visualBEER310

Member
Joined
Aug 31, 2014
Messages
6
Programming Experience
1-3
No it won't! Not unless you're actually starting a new thread inside the RecursiveSearch method and you've never said that that's the case and your code doesn't indicate that that the case. If you are starting a thread and then calling RecursiveSearch on that thread then there's no issue. All you have to do is follow my simple instructions. Wherever you are now calling RecursiveSearch you simply call this other method and in that method you call RecursiveSearch and then set the Boolean.

Now, if you actually ARE starting another thread inside the RecursiveSearch method then I have to wonder why you've never actually stated that or shown us the relevant code. Maybe you should just show us the code anyway, so that we can see for ourself either way. Show us the entire RecursiveSearch method and the entire method that calls it. Either you have been wasting time by denying something will work when you could have just done and seen that it works for yourself or you've been wasting time by not providing all the relevant information.
I FIXED IT!!! :) :D
I thought about what you had said and I now see what you meant. It works great. Thanks!!
 
Top Bottom