Dir() being annoying

JamesMcS

Keyboard-Chair Interface
Local time
Today, 22:07
Joined
Sep 7, 2009
Messages
1,819
Afternoon all!! Hoping you can point me in the right direction here....

So - I've got this bit of code. It's supposed to read the paths of folders and subfolders into an array, which I'm then going to cycle through with another Dir command to find MDBs and compact them.

The process is:

Call the sub with the starting folder as an argument, eg "C:\Temp"

Dir goes through files, looking for directories and ignoring . and ..

Finds a directory - calls itself again to repeat the process, appending paths of directories to the arrray as it goes.

It seems to work OK until it returns to the first instance of the sub after calling itself the first time. The Dir() command at the bottom returns "Invalid Procedure Call or Argument", which I take to mean that Dir is not looking where it was before the routine was called again... any ideas how I can get around this? It seems like such a simple piece of code to get the folder list, shame to have to expand it dramatically.....
Code:
Sub List_SubDirs(UserPath As String)
Dim SubDirList, Sub_Sub As String
Dim DirArrayPos As Integer
Call Init_Globals

DirArrayPos = 1
'Collect Root Subdirectory Names into Array
If DirArray(DirArrayPos) <> "" Then

SubDirList = Dir(DirArray(DirArrayPos) & "\*.*", vbDirectory)

Else

SubDirList = Dir(UserPath & "\*.*", vbDirectory)

End If

Do While SubDirList <> ""
    
    If GetAttr(UserPath & "\" & SubDirList) = vbDirectory And SubDirList <> "." And SubDirList <> ".." Then
            
            DirArray(DirArrayPos) = UserPath & "\" & SubDirList
            Sub_Sub = DirArray(DirArrayPos)
            DirArrayPos = DirArrayPos + 1
           
            Call List_SubDirs(Sub_Sub)
    
    End If
    
SubDirList = Dir

Loop

End Sub
 
First alarm bell is:
Call Init_Globals
is processed everytime the sub calls itself.
 
Ah that one just declares the array so I can use it in another routine
 
Does anyone know if it's possible to move the 'pointer' when using Dir() so that it skips the first n files it's supposed to return?
 
My question is - why go through the trouble to add them to an array, just to cycle through them again? Why not do what you want to do the first time through? Also, instead of using DIR, why not use the file system objects which can then retain information and you can move on as you should:
Code:
    Dim fso    As Object
    Dim fldr   As Object
    Dim sbfldr As Object
    Dim f      As Object
 
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldr = fso.GetFolder(UserPath)
 
    For Each f In fldr.Files
        If Right(f.Name, 3) = "mdb" Or Right(f.Name, 5) = "accdb" Then
            Debug.Print f.Path
        End If
    Next
 
    For Each sbfldr In fldr.SubFolders
        For Each f In sbfldr.Files
            If Right(f.Name, 3) = "mdb" Or Right(f.Name, 5) = "accdb" Then
                Debug.Print f.Path
            End If
        Next
Next

And that will print out the path/name of the file so you should be able to take it from here.
 
Last edited:
james

not quite sure what you are doing, (I think you are trying to copmile a entire tree of folders and files) but DIR() is definitely non-recursive. See F1 help on this topic

so you cannot have DIR iterate files in a folder, and when it finds a subforlder, iterate files in the subfolder, and then carry on where it left off in the higher level folder.

Unfortunately!
 
But you could reinitialize dir at the level you returned to, and skip stuff already known.

Otherwise, the FileSystemObject (Microsoft Scripting Library must be included in the references) could possibly provide you with some functionality

Update: Damn . this is what happens whan you don't read previous posts - Bob is into the same thing.
 
the FileSystemObject (Microsoft Scripting Library must be included in the references)
No it doesn't need to be included in the references. You can use it with late binding. It helps with having Intellisense but you don't have to have it as a reference.
 
Thanks for looking everyone - Bob's is definitely the more logical way to go, over mine, obviously....

I did have a bit of a brainwave about making Dir pick up where it left off, by calling it again into another variable, then when I returned to the original instance of the routine, comparing subdirlist to that varable in a loop until it matched, then continuing through the routine.
 

Users who are viewing this thread

Back
Top Bottom