Results 1 to 7 of 7

Thread: Recursive File List function

  1. #1

    Thread Starter
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375

    Recursive File List function

    I have a function that will return any files in a given folder. But in order to make it also return files in subfolders, i was told i would have to make it recursive. Recursive functions are something ive never had to use yet...and quite frankly i dont understand them. Could someone tell me what modifications i would have to make to this function to make it return subfolder files as well?
    VB Code:
    1. Function DirList(strDir As String) As String()
    2.     'returns a 0 based array with the files in strDir
    3.     'Can contain a pattern such as "*.*"
    4.     Dim Count As Integer
    5.     Dim sFiles() As String
    6.     Dim sFile As String
    7.    
    8.     sFile = Dir$(strDir)
    9.     Count = -1
    10.     Do
    11.         Count = Count + 1
    12.         ReDim Preserve sFiles(Count)
    13.         sFiles(Count) = sFile
    14.         sFile = Dir$
    15.     Loop
    16.     DirList = sFiles
    17. End Function
    thanks,
    Nishant
    You just proved that sig advertisements work.

  2. #2
    Hyperactive Member abhid's Avatar
    Join Date
    Nov 2001
    Location
    3rd rock from the sun
    Posts
    467
    recursive function is a function which calls itself untill a break condition.
    you must check whether file u have searched is a dirctory and if it is a dirctory, call your function again with new dirctory name. repeat this untill all the files have been searched.
    u must provide a break condition for recursive funtion otherwise it is a infinite loop

  3. #3

    Thread Starter
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    I see...could you post an example of a recursive function so i can look at it? I almost get the idea....but im not quite there. The calling itself thing just doesnt click in.
    You just proved that sig advertisements work.

  4. #4
    Hyperactive Member abhid's Avatar
    Join Date
    Nov 2001
    Location
    3rd rock from the sun
    Posts
    467
    i havent used recursive funtion in vb though
    simple example would belike finding a factorial of a number


    private function factorial(n as long,ans as long) as long
    if n = 1 then
    factorial = ans ' Check for break condition
    else
    ans = n * (n-1)
    factorial (n-1,ans) ' This is where a recursive call is made
    endif
    end funtion

  5. #5

    Thread Starter
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    I think i understand...but how would i implement that into my File-Listing function?
    You just proved that sig advertisements work.

  6. #6
    Hyperactive Member abhid's Avatar
    Join Date
    Nov 2001
    Location
    3rd rock from the sun
    Posts
    467

  7. #7
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    Heres an example of a recursive function.
    VB Code:
    1. Function Down2One(Number)
    2.  Down2One = Number - 1
    3.  Do Until x < 1
    4.   Down2One = Down2One(Number - 1)
    5.  Loop
    6. End Function
    What that function does is call itself over and over decreasing
    number
    by 1 each time until it is less than 1
    Luke

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