Results 1 to 22 of 22

Thread: get every folder on the drive! recurse sub folders directories

  1. #1
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 01
    Posts
    1,048

    Smile get every folder on the drive! recurse sub folders directories

    nice n easy..

    first click project -> references, and check Microsoft Scripting Runtime

    Win 98 SE and before will need a newer version of scrrun.dll

    VB Code:
    1. Public Function GetFolders(sStartFolder As String) As String
    2.     On Error Resume Next
    3.     Dim fso As New FileSystemObject
    4.     Dim f As Folder
    5.     Dim fldr As Folder
    6.     Set fldr = fso.GetFolder(sStartFolder)
    7.     GetFolders = fldr.Path & vbCrLf
    8.     If fldr.SubFolders.Count > 0 Then
    9.         For Each f In fldr.SubFolders
    10.             GetFolders = GetFolders & GetFolders(f.Path)
    11.         Next
    12.     End If
    13.     If Err.Number = 92 Then GetFolders = vbNullString
    14.     'windows did not allow the program to look inside this folder, so ignore it completely  (ie c:\system volume information in win2000)
    15. End Function

    note - the code got an edit recently... the following posts may or may not make sense
    Last edited by dis1411; Nov 5th, 2004 at 02:30 PM.

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 03
    Location
    UK
    Posts
    1,090
    can't you simply use the MS-DOS comand: tree
    type:

    tree /a >tree.txt

    when you're on the C:\ in MSDOS prompt, then (using windows) open C:\tree.txt

  3. #3
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 01
    Posts
    1,048
    well good luck converting that output into something your program can use

  4. #4
    New Member
    Join Date
    Nov 03
    Posts
    10
    Could this code be alter to where it would read in every file in a directory?
    I don't sufer from insanity, I enjoy it.

  5. #5
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 01
    Posts
    1,048
    yes
    add a file list (file1) to the project and set its visible property to false

    VB Code:
    1. Dim PathArr() As String
    2. Dim TempDir As String
    3.  
    4. Private Sub Command1_Click()
    5.         TempDir = "C:\"
    6.  
    7.         PathArr() = Split(GetFolders(TempDir), vbCrLf)
    8.        
    9.         For subs = 1 To UBound(PathArr)
    10.             File1.Path = PathArr(subs)
    11.             If File1.ListCount > 0 Then
    12.                 For files = 0 To File1.ListCount - 1
    13.                     'add PathArr(subs) & "\" & File1.List(files) to something (textbox etc)
    14.                 Next
    15.             End If
    16.         Next
    17. End Sub

  6. #6
    New Member
    Join Date
    Nov 03
    Posts
    10
    Sweet that helps alot big TY.
    I don't sufer from insanity, I enjoy it.

  7. #7
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 03
    Location
    Utah
    Posts
    1,068

    Re: get every folder on the drive! recurse sub folders directories

    Originally posted by dis1411

    note - the returned string will contain an extra vbCrLf at the beginning.. its not that bad cuz most likely youll split the string into an array and you can start at 1 instead of 0 when processing it
    Just change your code one tiny bit to fix that, why don't you...
    VB Code:
    1. Private Function Getfolders(sStartFolder As String) As String
    2.     Dim fso As New FileSystemObject
    3.     Dim f As Folder
    4.     Dim fldr As Folder
    5.     Set fldr = fso.GetFolder(sStartFolder)
    6.     Getfolders = fldr.Path & VbCrLf
    7.     If fldr.SubFolders.Count > 0 And Getfolders <> "" Then
    8.         For Each f In fldr.SubFolders
    9.           Getfolders = Getfolders & Getfolders(f.Path)
    10.         Next f
    11.     End If
    12. End Function

  8. #8
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 01
    Posts
    1,048
    yeah, but then youre left with an extra vbCrLf at the end..

    the when you split the result into an array it's going to be the same size either way, but the vbCrLf will be at the beggining or the end

  9. #9
    Super Moderator manavo11's Avatar
    Join Date
    Nov 02
    Location
    Other side of town from si_the_geek
    Posts
    7,171
    yeah, but then youre left with an extra vbCrLf at the end..
    Then at the end do :

    VB Code:
    1. Getfolders = Left$(Getfolders, Len(Getfolders) - 2)


    Has someone helped you? Then you can Rate their helpful post.

  10. #10
    Frenzied Member
    Join Date
    Sep 99
    Location
    Phoenix, az
    Posts
    1,517

  11. #11
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 01
    Posts
    1,048
    Originally posted by manavo11
    Then at the end do :

    VB Code:
    1. Getfolders = Left$(Getfolders, Len(Getfolders) - 2)
    obviously, yes

    the point was that something extra has to be done either way

  12. #12
    Super Moderator manavo11's Avatar
    Join Date
    Nov 02
    Location
    Other side of town from si_the_geek
    Posts
    7,171
    Originally posted by dis1411
    obviously, yes

    the point was that something extra has to be done either way
    Yeah, well, I don't see a problem in doing something extra since in both ways it would be needed anyway...


    Has someone helped you? Then you can Rate their helpful post.

  13. #13
    Frenzied Member
    Join Date
    Sep 99
    Location
    Phoenix, az
    Posts
    1,517
    hm

  14. #14
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 01
    Posts
    1,048
    Originally posted by manavo11
    Yeah, well, I don't see a problem in doing something extra since in both ways it would be needed anyway...
    perhaps you took me wrong. i wasnt complaining that something extra has to be done, i was stating it.

    if anyone were to have a 'problem' with taking out the vbcrlf, then their program would also problems

  15. #15
    Super Moderator manavo11's Avatar
    Join Date
    Nov 02
    Location
    Other side of town from si_the_geek
    Posts
    7,171
    Sorry, I misunderstood you... Anyway, all solved now


    Has someone helped you? Then you can Rate their helpful post.

  16. #16
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 02
    Location
    At work...
    Posts
    648
    There is a better way of doing this that does not include a reference to the scripting runtime lib. I once found it on these forums but I have now lost the idea.

    Basically it is also a recorsive function, the first part populates an array of folders.For each folder, the list of files are returned. Next folder type of style...

    If any1 has it then cool - please post the code or link here

  17. #17
    Super Moderator manavo11's Avatar
    Join Date
    Nov 02
    Location
    Other side of town from si_the_geek
    Posts
    7,171


    Has someone helped you? Then you can Rate their helpful post.

  18. #18
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 02
    Location
    At work...
    Posts
    648
    Thanks manavo

    you hit the nail on my head!!

    I

  19. #19
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 01
    Posts
    1,048
    for some reasion when i posted this i liked the FSO way over the Dir way.. i can't remember why anymore

  20. #20
    Super Moderator manavo11's Avatar
    Join Date
    Nov 02
    Location
    Other side of town from si_the_geek
    Posts
    7,171
    Originally posted by MikkyThomeon
    Thanks manavo

    you hit the nail on my head!!

    I


    dis1411, probably cause of the reference needed for FSO


    Has someone helped you? Then you can Rate their helpful post.

  21. #21
    Hyperactive Member
    Join Date
    Apr 04
    Posts
    342
    I seem to have found that Dir() seems to leave the directories open after you use it in Windows XP, so I changed all my Dirs to Use the FindFirst/FindNext/FindClose API Calls.....
    Attached Files Attached Files

  22. #22
    New Member
    Join Date
    Jan 06
    Posts
    2

    Re: get every folder on the drive! recurse sub folders directories

    very goooooooooood
    thanks

Posting Permissions

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