Results 1 to 5 of 5
  1. #1
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Traverse dir-tree & create files (VB6)

    I need to create a little tool that traverses a directory tree and creates a file in each subdirectory in which all files in that subdir are listed.

    I'm using VB6 and have created my tool using the DIR statement but think that the fact that I'm creating a new file in each subdir while DIR is being used in my programming-loop is creating errors.

    Are there some tips or templates I could use to do this; I did look around a bit but couldn't quite find what I was looking for. Thanks!

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Traverse dir-tree & create files (VB6)

    The code in <post:=319,305>post 319,305</post:> shows how you can use Dir recursively.

    Perhaps the code from [VBnet File API] FindFirstFile: Recursive Search for Folders Using a Folder Mask (minimal code) (pointed out by <!profile=Don Ceraso>Don Ceraso<!/profile>) can be of use too.

  3. #3
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Traverse dir-tree & create files (VB6)

    Back to this again... I am trying this code
    <pre>Function RecurseFilesAndFolders(strPath)
    Dim SubFolders As Collection
    Dim strTemp as string
    dim varTemp
    Set SubFolders=New Collection
    strTemp=Dir(strPath,vbDirectory)
    Do Until strTemp=""
    If strTemp<>"." and strTemp<>".." Then
    If GetAttr(strPath & strTemp) And vbDirectory Then
    SubFolders.Add strTemp
    Else
    'Do what you want, it's a file....
    End if
    End if
    strTemp=Dir()
    Loop
    For Each varTemp in SubFolders
    RecurseFilesAndFolders(strPath & varTemp & "")
    Next
    End Function
    </pre>


    But still don't get it.. I want to populate an array with the pathnames of all subfolders and get that out. In the code above, I see that the "SubFolders" collection is recursively populated, I get that much. At the same time, this collection is local to the procedure so I cannot get it out. I don't believe I can simply replace the collection with e.g. a globally defined array as the collection is used for recusing... anyone? <img src=/S/bananas.gif border=0 alt=bananas width=33 height=35>

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Traverse dir-tree & create files (VB6)

    Here is an example:
    <code>
    ' Module-level collection to store all subfolder names in
    Dim colFolders As Collection

    Sub Test()
    Dim varItem As Variant
    ' Initialize colFolders
    Set colFolders = New Collection
    ' Add all subfolders of C:MyFiles to colFolders
    RecurseFolders "C:MyFiles"
    ' Do something with the collection - in this example, just list them
    For Each varItem In colFolders
    Debug.Print varItem
    Next varItem
    End Sub

    Sub RecurseFolders(ByVal strPath As String)
    Dim colSubFolders As New Collection
    Dim strSubFolder As String
    Dim varItem As Variant
    If Not Right(strPath, 1) = "" Then
    strPath = strPath & ""
    End If
    strSubFolder = Dir(strPath, vbDirectory)
    Do Until strSubFolder = ""
    If strSubFolder <> "." And strSubFolder <> ".." Then
    If GetAttr(strPath & strSubFolder) And vbDirectory Then
    ' Add to both local and global collections
    colFolders.Add strPath & strSubFolder
    colSubFolders.Add strSubFolder
    End If
    End If
    strSubFolder = Dir
    Loop
    For Each varItem In colSubFolders
    RecurseFolders strPath & varItem
    Next
    End Sub</code>

  5. #5
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Traverse dir-tree & create files (VB6)

    You're great! <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

Posting Permissions

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