Method to get ALL subdirectories from all subdirectories.

aewarnick

Well-known member
Joined
Jan 29, 2003
Messages
1,031
All that I have seen are methods to get directory info and its immediate subdirectories but what about the other subdirectories beneath? Is there a method to get all the levels of subdirectories from a parent dir?
 
It is easy to go down a set number of levels but I could not think of a universal method for any level. Can you think of any ideas of how to do it?
 
Its called recursion....


Sub myDirs (topLevel as directory, foundSoFar as collection)

for each dir in topLevel
foundSoFar.Add dir
Call myDirs(dir)
next Dir


End sub

Pseudocode, but I hop you get the idea.
 
I thought that would be the way to do it but I have never used it before. I will have to do some testing and studying.
 
example on how to add it to, eg a treeview, when entering a path into a textbox and clicking on a button:
Code:
 Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
        TreeView1.Nodes.Clear()

        ProcessDirectories("c:\path....", TreeView1.Nodes)

End Sub

Private Sub ProcessDirectories(ByVal path As String, ByVal parentCollection As TreeNodeCollection)
        Dim items As String() = IO.Directory.GetDirectories(path)
        Dim item As String
        Dim itemNode As TreeNode
        For Each item In items
            itemNode = parentCollection.Add(item)
           this is the recursion
            ProcessDirectories(item, itemNode.Nodes)
        Next

End Sub
I know its in vb, but im sure you can get to a translation
 
Objects that are not value types are passed by reference regardless.
 
That may take a long time

I tried that on an old app, and I took forever to load the TreeView at runtime. You need to consider that people may have HUGE directory structures. I think you can do it faster by working with the Windows Shell but I am not sure
 
I agree. If you *are* doing this to put in a tree control or something you should just expand it on an as-needed basis. via the BeforeExpand event for example.
 
recursion is very confusing. It looked logical at first but when I put in this code, it works and I have no idea why.

Code:
public static string[]GetAllFiles(string Dir)
		{
			DirectoryInfo Di=new DirectoryInfo(Dir);
			FileInfo[]FArr=Di.GetFiles();
			
			for(int j=0; j<FArr.Length; j++)
			{
				o+=FArr[j].FullName+"\n";
			}
			string[]SubDirs=Directory.GetDirectories(Dir);
			foreach(string D in SubDirs)
			{
				o+="1\n";
				GetAllFiles(D);
			}

			char[]sp=new Char[]{\n};
			string[]AllDirectories=o.Split(sp[0]);
			o="";
                                                return AllDirectories;
		}

I need to clear static string o but I cannot put it in the code anywhere because it erases too early and that is one thing I dont understand.

Does a recursive call do the entire method each time it is called?
 
Yep. I understand it now after playing with it for a while I see how it works. But it pretty much locks up when I try to get windows. I left for over an hour and it still was not done. Do you think that is normal or is my code just extremely inefficient?

I wanted a way to immediately put it into an array instead of a huge string but I could not see any way to do it because I dont know how many files are in all those directories so that I can size the array properly.

Also, when the computer is processing it it barely works! Is there any way to make it really work hard to speed the process up? Maybe putting the method on a separate thread? My other post is about that.
 
What do you want to use this for? If you are creating some sort of windows explorer like application, you must go with the design quwiltw suggested (expand on demand), and not load everyting at once.
 
Nope, it is not an explorer like inteface at all, the user will not even know what is going on, it will run about every 2 minutes and open files to read them and add to them a certain mark when the time is right. It is a security thing. So I need them all at once. It will not be near as long as windows but I think it still needs to be faster.
 
Back
Top