traversing directory tree

oaishm

Board Regular
Joined
Jan 30, 2009
Messages
97
I was looking to traverse the directory tree on the web, but the code I've seen is usually in excess of 50 lines, whereas other languages do this in five lines. Before I dedicate two days to figure it out, I made my own, which is almost there, but I have two problems with it
Code:
Public pointer As Range
Sub init()
    Set pointer = Worksheets("Sheet1").Range("A1")
    mypath = "l:\CRATING\CUSTOMERS\Martin Customers"
    produce (mypath)
End Sub
Sub produce(mypath)
   file = Dir(mypath & "\*", vbNormal Or vbDirectory)
    Do While file <> ""
        If Not (file = "." Or file = "..") Then
            If GetAttr(mypath & "\" & file) = vbDirectory Then
                produce (mypath & "\" & file)
            Else
                pointer.Value = file
                pointer = pointer.Offset(1, 0)
            End If
            
        End If
        file = Dir("")
    Loop
End Sub

I'm trying to use pointer as a global variable, but in the produce subroutine, it makes its own pointer variable. Do I have to declare global pointer like in php?

When I hit file=Dir(""), it returns the same exact file it's already on. When I do file = Dir I get an error Invalid prodedure call or argument as if it forgot the items I loaded into it in the first place

I've also seen a lot of this to check if its a directory:
GetAttr(something, vbDirectory) And vbDirectory

why so difficult? Doesnt' GetAttr just naturally return the attribute, which should be vbDirectory?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I must be the world's worst coder, but here's solution that solved my three problems:
1. vb doesn't have a push concept and can't redim in random situations like mine
2. the Dir function is one global function, when an iterative process is made, a new dir doesn't get instantiated so the pointer is all over the place
3. I don't have the time to figure out global variables so I used selection as a pointer instead

Code:
Sub init()
    Worksheets("Sheet1").Range("A1").Select
    mypath = "l:\CRATING\CUSTOMERS\Martin Customers"
    produce (mypath)
End Sub
Sub produce(mypath)
   goods = getgoods(mypath)
   i = 0
      Do While goods(i) <> ""
            If Not (goods(i) = "." Or goods(i) = "..") Then
                If GetAttr(mypath & "\" & goods(i)) = vbDirectory Then
                    produce (mypath & "\" & goods(i))
                Else
                    Selection.Value = goods(i)
                    Selection.Offset(1, 0).Select
                End If
            End If
            i = i + 1
        Loop
End Sub
Function getgoods(path)
Dim directories(1000) 'Because vb has no real push function.  What a misfortune and redim doens' work in MANY circumstances so don't bother making a push function
i = 0
    file = Dir(path & "\*", vbNormal Or vbDirectory)
    Do While file <> ""
        directories(i) = file
        file = Dir
        i = i + 1
    Loop
    getgoods = directories
End Function

When I googled potential answers, they didn't work either. I can't be the only one having such a hard time
 
Upvote 0
Example code for traversing directory tree - http://www.ammara.com/access_image_faq/recursive_folder_search.html. This uses the Dir function and a Collection object to store the list of folders. A shorter program and possibly faster, depending on what you want to do, can be written using FileSystemObject.

For the GetAttr question, the VB Help explains why you need to perform a bitwise And to determine if a particular attribute is set.
 
Upvote 0
Thanks, the collection object sounds like a good hour of learning to figure out. Well, my ridiculous hack did the trick. Your link does finally offer the telling ridiculousness "due to Dir's internal implementation it can't be called recursively. "

Meanwhile, every other modern file system object has figured out stacks.
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,909
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top