Results 1 to 2 of 2

Thread: Recursive sub dirs using Dir()

  1. #1

    Thread Starter
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828

    Recursive sub dirs using Dir()

    hello all.

    ive done a search on this one and come up with various results but nothing solid. most of them are using the api, i just want to write a simple recursive function to retrieve all subdirs and their subdirs etc and return their paths in a string array, thats all....

    only problem is that you cant use the Dir() function in a recursive...

    i also dont want to use the filesytemobject as its way too much overhead...

    any advice would be lovely..

    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    You can use the Dir function you just have to be a bit clever with it.

    To look at a recursive object you need to act recursive youself.

    Create 1 function that you pass a path to, that then looks at the suggest path and gets a list of all the files. Do a quick test to see if the file is a folder, if so put into a Folders array, else put into a file array.

    Then go through the folder array and call yourself to get the data back (the recursion), it will auto-follow all sub dirs and come back with an entire list.

    Compress at end, and send back to caller.

    All you have to do to use it, is call this or the like

    strArray() = funcGetFiles( "C:")


    VB Code:
    1. Dim strDocArray(0) as string
    2. Dim strFolderArray(0) as string
    3.  
    4. Function funcGetFiles(FilePath as String) as String()
    5.  
    6. strThing = Dir(FilePath & "\*")
    7.  
    8. Do until strThing = ""
    9.  
    10.    If strThing ISFOLDER   'FORGET CODE OFF TOP OF HEAD then
    11.       strFolderArray(Ubound(strFolderArray)) = strThing
    12.       Redim strFolderArray(Ubound(strFolderArray)+1)
    13.   Else
    14.       strDocArray(Ubound(strDocArray)) = strThing
    15.       Redim strDocArray(Ubound(strDocArray)+1)
    16.   End If
    17.   strThing = Dir
    18.  
    19. Loop
    20.  
    21. 'Now go through the folders
    22.  
    23. Dim FinalList() as String
    24. Dim strFolderPaths() as string
    25.  
    26. Dim intIndex as Integer
    27.  
    28. For intIndex = 0 to Ubound(strFolderArray) - 1
    29.  
    30.    'The all important call to yourself, dont mess up or you will get an overflow !
    31.    strFolderPaths= funcGetFiles(  strFolderArray(intIndex)   )
    32.  
    33.    Dim intIndex2 as Integer
    34.    For intIndex2 = 0 to Ubound(strFolderPaths) - 1
    35.        FinalList(Ubound(FinalList)) = strFolderPaths(intIndex2)
    36.    Next
    37.  
    38. Next
    39.  
    40.  
    41. 'Add the current list of files and folders as well !!
    42. blah blah FinalList 'ETC ETC
    43.  
    44. Return FinalList
    45.  
    46. End Function

    PS: This should be enough for you to go on, please dont make me do the whole thing .

    PPS. check for files called . and .. and removed them, Dir returns the dummy checks sometimes.

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