Click here to Skip to main content
15,306,245 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am aware of how to list all hard drives on my computer but am unaware of how to scan the selected directories folders and subfolders once the user has selected a parent node such as ("C:\"). Can someone please give a solution or example?

Thank you very much for your time and expertise! [Note: I have decided to repost my question and try to word it better.]
Posted
Updated 2-Feb-11 0:03am
v3
Comments
OriginalGriff 2-Feb-11 6:08am
   
Answer updated.
OriginalGriff 2-Feb-11 9:19am
   
Answer updated again.
OriginalGriff 2-Feb-11 10:09am
   
And again...

In a word: Recursion.
If you haven't met it before it is a simple idea, that is best explained by:

Recursion: see "Recursion"

Google for it, and read up on what it says.

The basic idea is:
private void button1_Click(object sender, EventArgs e)
    {
    LookAtDir(@"F:\Temp");
    }

private void LookAtDir(string path)
    {
    foreach (string dir in Directory.GetDirectories(path))
        {
        LookAtDir(dir);
        }
    ...Do something with the directory.
    }


VB
Private Sub button1_Click(sender As Object, e As EventArgs)
    LookAtDir("F:\Temp")
End Sub
Private Sub LookAtDir(path As String)
    For Each dir As String In Directory.GetDirectories(path)
        LookAtDir(dir)
    Next
    '...Do something with the directory.
End Sub


[edit]Sorry - I forgot you were VB - OriginalGriff[/edit]



"Ok I have added this to my treeview and all seems well. I had to add a try catch statement to your private sub LookAtDir but now I am unsure it is doing anything at all. Can you tell me how to itterate through the folders and subfolders to show the current folder scanned in a textbox? so that i can see this in action.........."

This can get complex: Bear in mind that it will slow things down LOTS to show each of your folders and sub-folders as you process it, and unless you move the processing code to a background thread or do something nasty you won't see anything happening on screen.

To move it to a background thread, search the articles on this site, I know there are a couple of very good ones: I think Luc Pattyn, Pete O'Hanlon, and DavyM69 will be a good place to start, but it's been a while since I looked at them.

To do the nasty, call the Application.DoEvents method after every update to your TextBox. Nasty, and will slow things down a lot, but it'll be ok for testing.

Alternatively, for testing output the current path to the console...
"Ok once again sorry for my ignorance but will you show me in writing what you mean by doing the nasty? lol......... I am new to console and some of the terms you have used here. also I would love it if you could explain the part of your code that points to the dir.... LookAtDir("F:\Temp"). How can I enable the user to click any root dir or folder/subfolder and make it available to scan?.... I know that theese are not easy questions and know that I may not be asking the correct questions but I think you get the idea where id like to go with this..... In short I want a treeview that can show all hard drives and there folders/subfolders. The user then clicks the desired root such as C:\ or D:\ ect and clicks start........ The textbox then shows the current folder being scanned untill eof...."

Doing it the nasty way, instead of the "...Do something with the directory." bit above:
VB
textBox1.Text += dir
Application.DoEvents()


It is nasty though, and should only be for testing. Threading is a much better solution for the real world.

Console: If you create a console app, you can do Console.WriteLine to print to the "screen", yes? It works in WinForms apps as well - the output goes to the "output" pane of the debugger...

All the routine LookAtDir needs is the text path to the directory. So if you pass it "F:\" (the root of the 'F' drive), it will scan it and call itself with a new path for each folder within the root drive of 'F'. So if you have the following structure:
F:\
   Temp
      Folder1
   Perm
      Folder2
      Folder3
then LookAtDir will be called with:
LookAtDir("F:\")
LookAtDir("F:\Temp")
LookAtDir("F:\Temp\Folder1")
LookAtDir("F:\Perm")
LookAtDir("F:\Perm\Folder2")
LookAtDir("F:\Perm\Folder3")
So all you have to do is pass the path to the top level folder you want to scan, and it will handle all the ones below. If you called
LookAtDir("F:\Perm")
then it would just process
LookAtDir("F:\Perm")
LookAtDir("F:\Perm\Folder2")
LookAtDir("F:\Perm\Folder3")
and ignore all the others.
   
v4
Comments
Dale 2012 2-Feb-11 6:53am
   
thank you very much for this. I know of recrusion but for some reason was only thinking of itteration. :) this has brought light to my situation thank you again very much
Dale 2012 2-Feb-11 9:07am
   
Ok I have added this to my treeview and all seems well. I had to add a try catch statement to your private sub LookAtDir but now I am unsure it is doing anything at all. Can you tell me how to itterate through the folders and subfolders to show the current folder scanned in a textbox? so that i can see this in action..........
Dale 2012 2-Feb-11 9:56am
   
Ok once again sorry for my ignorance but will you show me in writing what you mean by doing the nasty? lol......... I am new to console and some of the terms you have used here. also I would love it if you could explain the part of your code that points to the dir.... LookAtDir("F:\Temp"). How can I enable the user to click any root dir or folder/subfolder and make it available to scan?.... I know that theese are not easy questions and know that I may not be asking the correct questions but I think you get the idea where id like to go with this..... In short I want a treeview that can show all hard drives and there folders/subfolders. The user then clicks the desired root such as C:\ or D:\ ect and clicks start........ The textbox then shows the current folder being scanned untill eof....
Dale 2012 2-Feb-11 22:49pm
   
I am still unable to perform this correctly because i need to allow the user to select any root/folder/subfolder to scan. your example shows "lookatdir F:\" which may or may not be what the user decides to scan. Also I would like to see the current folder being scanned in the lblmessage.text so that I can see where the scan is and when it may be complete. I have posted my full code of what i have so far at http://www.codeproject.com/Questions/153076/Scan-folders-and-subfolders-with-treeview-all-most.aspx Will someone if not yourself look at my code to make adjustments if any errors exist and maybe if possible add to it so that it works that way ive explained?...........

Thank you very much in advance and hope to hear from you sooooon :)
Have a look at System.IO.Directory.GetFiles and System.IO.Directory.GetDirectories Methods. Make a recursive function to query all child folders. But think about bad performance - If you have a very deep nested or big filesystem. When I created an "Explorer" with a treeview I just created the nodes for another two "levels" down from the user selection - one would be enough but so the user sees if there are any more child folders (the treeview then shows the little + sign)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900