micropathic Posted February 27, 2004 Posted February 27, 2004 Hi, I need to figure out how to get a list of all of the contents (fiiles and folders) of a given directory including what's in its subdirectories. I plan on populating a listbox with this info. I've searched the forum for some type of solution, but it seems most of what I had found had to do with using a recursive function to populate a treeview and I can't really figure out how to use that code the way I need it. So, any help would be super appreciated! Thanks for any help! Quote
betrl8thanever Posted February 27, 2004 Posted February 27, 2004 Hello micropathic, I put this example together really quickly to kind of show you how to recursively search a directory or drive. I'm just adding this data to a richtextbox, but I think you'll get the idea. Dim mainDir As String = "D:\" Dim mainDirStr As String Dim f As String If Directory.Exists(mainDir) Then For Each mainDirStr In Directory.GetDirectories("D:\") Me.RichTextBox1.Text = Me.RichTextBox1.Text & mainDirStr & vbCrLf For Each f In Directory.GetFiles(mainDirStr) Me.RichTextBox1.Text = Me.RichTextBox1.Text & f & vbCrLf Next Next End If Quote "In the immortal words of Socrates, who said "' I drank what?!'"
micropathic Posted February 28, 2004 Author Posted February 28, 2004 That was exactly what I needed. Thank You! Quote
micropathic Posted February 28, 2004 Author Posted February 28, 2004 Oops... I just noticed that this code only recurses through 1 level of subdirecories (it doesn't get the subdirectories of the subdirectories). I've been playing around with the code a little bit more, but cannot seem to get it work. Do you know of anything I can do? Thanks! Quote
micropathic Posted February 28, 2004 Author Posted February 28, 2004 I think I may have discovered the answer to my question here: http://www.vb2themax.com/Item.asp?ID=807&PageID=CodeBank Quote
betrl8thanever Posted February 29, 2004 Posted February 29, 2004 That will do. If you run in to any more issues let me know. Quote "In the immortal words of Socrates, who said "' I drank what?!'"
Arch4ngel Posted April 26, 2004 Posted April 26, 2004 Recursive function Watch out for recursiv function as they quickly overload the stack. Because a function doesn't get removed from the stack as long as it's not finish... if you recall to much it's gotta give an error. (Stack overflow I think) Private Sub GetDirectories(string str ) Begin Dim mainDir = str if( Directory.GetDirectories( str ).Length > 0 ) Then [font=Courier New][color=black]If Directory.Exists(mainDir) Then For Each mainDirStr In Directory.GetDirectories("mainDir[/color][/font][font=Courier New][color=#dd0000][color=black]") Me.RichTextBox1.Text = Me.RichTextBox1.Text & mainDirStr & vbCrLf [/color][/color][/font] [font=Courier New][color=#dd0000][color=black] GetDirectories( mainDirStr ) For Each f In Directory.GetFiles(mainDir) Me.RichTextBox1.Text = Me.RichTextBox1.Text & f & vbCrLf Next[/color][/color][/font] [font=Courier New]End If[/font] [font=Courier New][color=#dd0000][color=#000000]End Sub[/color] [/color][/font] Didn't test it. So it may have bugs. Be sure not to fall in an infinite loop. N.B. : We learn at school that it's better NOT to use recursive and they are only needed in very few case. THIS case is one of them. Stay Zen. This kind of programming is sorted as advance and should be used wisely. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
Arch4ngel Posted April 26, 2004 Posted April 26, 2004 http://www.codeproject.com/csharp/RecursiveFileExplorer.asp There's a more complete way of doing a search there. Stay Zen. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.