Looping thru Directory/Subdirectory Structure

BlakeMcKenna

Active member
Joined
Oct 27, 2008
Messages
38
Programming Experience
10+
I have an application in which it is supposed to open and search every file for a specified string. In other words if the starting path is "C:\", the program needs to loop thru every directory under C:\ and every subdirectory.

How can I do this?

Thanks,
 
VB.NET:
	Private Sub ExamineDir(ByVal Dir As String)
		For Each Doc As String In Directory.GetFiles(Dir)
			'Your code to process the file
		Next

		For Each SubDir As String In Directory.GetDirectories(Dir)
			'Recursive call to process subfolders
			ExamineDir(SubDir)
		Next
	End Sub
 
Thanks guys...that worked.

JMC...you were right, I did encounter an exception. I just inserted this code within a Try Catch structure. It's all good!!!

Thanks again!
 
To do this without recursion, use a Queue or a Stack (depending if you want depth or breadth first search), and push directories onto it as you enter each dir, then scan every file. This process runs inside a loop that consumes the queue/stack
 
You never heard of a Queue or a Stack? Think about the last time you visited a canteen, picked up a tray, and waited for your turn to get served..
Stack, Queue.

For computer related infos (as its something every computer science student does) see Google
 
I have heard of the stack but know nothing about it or what it really does. I still need a code example of what your talking about though.
 
Well, you see.. you asked for code to loop through a directory structure, but you were actually given code to recurse through a directory structure

Recursion
Math 60 -- Notes A3: Recursion vs. Iteration

To avoid recursion, you'll need some other way to store knowledge of your position within the search, and what directories are left. The concept is simple.. you add directories as you find them, and consume them one at a time in a loop:

VB.NET:
push 1 dir onto the stack

while there are dirs on the stack
pop a dir off the stack
push all the subdirs onto the stack
search all the files
loop

It's even simpler than the recursion
 
Because I didn't need any specific order to my program (this was a while ago), i simply did the following using an array...

Get folders in C:\ and add to an array.

(0) C:\windows
(1) C:\Program Files

etc

and used a loop which goes through each element and gets the directories within that directory and puts them on the end of the array....

Probably not a good idea to use my method :D
 
and used a loop which goes through each element and gets the directories within that directory and puts them on the end of the array....

Probably not a good idea to use my method :D

Same method as I describe, only we use different data containers. Your "put them on the end of the array" is a Queue - First In First Out. If you'd put them on the start of the array, it would be a Stack - Last In First Out

Stack causes a depth first search:
c:
c:\a
c:\a\a
c:\a\b
c:\b
c:\b\a
c:\b\b


Queue causes a breadth first search:
c:
c:\a
c:\b
c:\a\a
c:\a\b
c:\b\a
c:\b\b

Stay away from using arrays if possible; redimming them is a very costly exercise. Use a container that can grow itself, Stack, Queue, List ..
 
Same method as I describe, only we use different data containers. Your "put them on the end of the array" is a Queue - First In First Out. If you'd put them on the start of the array, it would be a Stack - Last In First Out

Stack causes a depth first search:
c:
c:\a
c:\a\a
c:\a\b
c:\b
c:\b\a
c:\b\b


Queue causes a breadth first search:
c:
c:\a
c:\b
c:\a\a
c:\a\b
c:\b\a
c:\b\b

Stay away from using arrays if possible; redimming them is a very costly exercise. Use a container that can grow itself, Stack, Queue, List ..

Cheers for that example, I've never thought of it from that angle :)

As for arrays, I use them because im used to them, and I need to push myself into doing things with better practice really. Im sure theres alot that .net can do which im going about the long way with.
 
Back
Top