Finding all folders path in all directories
Finding all folders path in all directories
Finding all folders path in all directories
Finding all folders path in all directories
Finding all folders path in all directories
Finding all folders path in all directories Finding all folders path in all directories Finding all folders path in all directories Finding all folders path in all directories Finding all folders path in all directories Finding all folders path in all directories Finding all folders path in all directories Finding all folders path in all directories
Finding all folders path in all directories Finding all folders path in all directories
Finding all folders path in all directories
Go Back  Xtreme Visual Basic Talk > > > Finding all folders path in all directories


Reply
 
Thread Tools Display Modes
  #1  
Old 07-19-2011, 06:12 AM
chongheng chongheng is offline
Newcomer
 
Join Date: Jul 2011
Posts: 8
Default Finding all folders path in all directories


i wanna list all the path of every folders contained in a main folder in a listbox

Here's my problem

For Each level1 As String In My.Computer.FileSystem.GetDirectories("C:\Documents and Settings\All Users\Desktop\database")
ListBox1.Items.Add(level1)
Next

For Each level2 As String In My.Computer.FileSystem.GetDirectories
(level1) <------------------------------- this part
ListBox1.Items.Add(level2)
Next

i cant seems to get the variable into the second level folder.

is there a better method to get all directory path than this?

pls help! =)
Reply With Quote
  #2  
Old 07-19-2011, 07:14 AM
passel's Avatar
passelFinding all folders path in all directories passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Looks like VB.Net to me, not legacy VB, so I moved the thread to the .Net side of the forums.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #3  
Old 07-19-2011, 08:34 AM
AtmaWeapon's Avatar
AtmaWeaponFinding all folders path in all directories AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

You have a lot of choices.

If you want all the files in no particular order, you can pass SearchOption.AllDirectories to Directory.GetFiles(). (I'm not sure if My.Computer.FileSystem has an equivalent; I don't use those because they don't scale to complicated scenarios.) It'd look something like this, assuming you import System.IO:
Code:
Dim allFilePaths() As String = Directory.GetFiles("...", "*.*", SearchOption.AllDirectories)
They won't be in any guaranteed order, so if you want more control you'll have to parse the paths and figure out where everything belongs.

The traditional technique for doing this is recursive. You write a method that can get all of the files in one directory, then have it call itself for any subdirectories. Here's one example:
Code:
Sub ListAllFiles(ByVal root As String)
    For Each subPath In Directory.GetDirectories(root)
        ListAllFiles(subPath)
    Next

    For Each filePath In Directory.GetFiles(root)
        Listbox1.Items.Add(filePath)
    Next
End Sub
This technique can cause a stack overflow on some deeply nested filesystems, but using a queue or stack it can be converted to an iterative approach.

.NET 4 also has EnumerateDirectories() and EnumerateFiles() that can be useful for making updates as you go; the GetXXX() methods wait until they have all files before returning. To use these properly, you have to involve a worker thread; it's usually a good idea for the GetXXX() methods too. That's a different post, if you do a few web searches you ought to find thousands of samples for this very common problem.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 07-20-2011, 08:12 AM
Gruff's Avatar
GruffFinding all folders path in all directories Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

chongheng,

You have posted other questions about file searching in the Legacy VB (VB4/5/6) forum area.
Now you are posting them in the VB.NET forum area. Which VB environment are you using?
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Finding all folders path in all directories
Finding all folders path in all directories
Finding all folders path in all directories Finding all folders path in all directories
Finding all folders path in all directories
Finding all folders path in all directories
Finding all folders path in all directories Finding all folders path in all directories Finding all folders path in all directories Finding all folders path in all directories Finding all folders path in all directories Finding all folders path in all directories Finding all folders path in all directories
Finding all folders path in all directories
Finding all folders path in all directories
 
Finding all folders path in all directories
Finding all folders path in all directories
 
-->