Results 1 to 6 of 6

Thread: directories

  1. #1

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    directories

    How can I iterate through all folders and its subfolders within a given directory.

    Ex.

    Say that I get c:\demo

    I want to find all subfolders within that folder and all subsequent ones.

    c:\demo\sub1\sub2\sub3\sub4 etc...

    Thanks,
    Don't anthropomorphize computers -- they hate it

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    I'm too lazy to tell how to do it, so I give the code right away And I'm too lazy to edit the file getting part out of it... but I'm sure you can modify it to store all the directories.

    VB Code:
    1. Dim DirList As New Collection, FileList As New Collection, Temp As String
    2.     On Error Resume Next
    3.     DirList.Add "C:\"
    4.     Do While DirList.Count
    5.         Temp = Dir$(DirList(1), vbDirectory)
    6.         Do Until Temp = ""
    7.             If Temp = "." Or Temp = ".." Then
    8.                 'do nothing
    9.             ElseIf (GetAttr(FixPathFile(DirList(1), Temp)) And vbDirectory) = vbDirectory Then
    10.                 DirList.Add FixPathFile(DirList(1), Temp) & "\"
    11.             Else
    12.                 FileList.Add FixPathFile(DirList(1), Temp)
    13.             End If
    14.             Temp = Dir
    15.         Loop
    16.         DirList.Remove 1
    17.     Loop
    18.     'now FileList contains all files

    VB Code:
    1. 'in a module
    2. Public Function FixPathFile(ByVal Path As String, File As String) As String
    3.     If Right$(Path, 1) <> "\" Then Path = Path & "\"
    4.     FixPathFile = Path & File
    5. End Function

  3. #3

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950
    This does not return anything.
    Don't anthropomorphize computers -- they hate it

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Spend time reading the code, don't just go ahead and paste. If you read the comment, you'd understand FileList contains a list of all files found in the directories. Now, it shouldn't be a big job to rip this functionality out and add functionality to add found directories to a listbox, for example.

  5. #5
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 2002
    Location
    At work...
    Posts
    648
    That's a cool way to do it!! I used to use a recursive function before - not anymore!!!

  6. #6
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width