Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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!

Posted

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

"In the immortal words of Socrates, who said "' I drank what?!'"
Posted
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!
  • 1 month later...
Posted

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.

"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

Posted

http://www.codeproject.com/csharp/RecursiveFileExplorer.asp

There's a more complete way of doing a search there.

 

Stay Zen.

"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...