Dir function

W

Warrio

Hello!

How come that we cannot use more than once the function Dir in the same
procedure?
I would like to browse with VBA, each folder of a drive and go into each
subdirectory with a loop

the code looks like:
myDir = Dir("C:\",vbDirectory)
do While myName <> ""
IF myDir <> "." and myDir <> ".." THEN
mySubDir= Dir("C:\" & myDir)
...
ENDIF
Loop

Thanks for any suggestion or alternative!
 
W

Wayne Morgan

From the help file:

To get any additional file names that match pathname, call Dir again with no
arguments. When no more file names match, Dir returns a zero-length string
(""). Once a zero-length string is returned, you must specify pathname in
subsequent calls or an error occurs. You can change to a new pathname
without retrieving all of the file names that match the current pathname.
However, you can't call the Dir function recursively. Calling Dir with the
vbDirectory attribute does not continually return subdirectories.

So, in the next call you would just use

mySubDir= Dir

You would do this until Dir returns "". Also, calling Dir with the
vbDirectory argument "Specifies directories or folders in addition to files
with no attributes". However, on my system when I tried this, I received
Autoexec.bat as one of the return values. The Autoexec.bat file has HSA
attributes, so according to the above, it shouldn't have been returned. This
appears to be because the file is 0 bytes. I was able to get additional
folders by calling the Dir function again without attributes. Once you get
the file/folder name, you'll need to check to see if it is a folder by
checking its Folder attribute.

If GetAttr("Path\FileOrFolderName") and vbDirectory > 0 Then
'the item has the folder attribute set
End If
 
A

Albert D. Kallal

Here is a recursive solution I use, and it runs very fast:

Sub dirTest()

' this is a test routine to see if my
' funciton works!

Dim dlist As New Collection
Dim startDir As String
Dim i As Integer

startDir = "C:\access\"
Call FillDir(startDir, dlist)

MsgBox "there are " & dlist.Count & " in the dir"

' lets printout the stuff into debug window for a test

For i = 1 To dlist.Count
Debug.Print dlist(i)
Next i

End Sub


Sub FillDir(startDir As String, dlist As Collection)

' build up a list of files, and then
' add add to this list, any additinal
' folders

Dim strTemp As String
Dim colFolders As New Collection
Dim vFolderName As Variant

strTemp = Dir(startDir)

Do While strTemp <> ""
dlist.Add startDir & strTemp
strTemp = Dir
Loop

' now build a list of additional folders
strTemp = Dir(startDir & "*.", vbDirectory)

Do While strTemp <> ""
If (strTemp <> ".") And (strTemp <> "..") Then
colFolders.Add strTemp
End If
strTemp = Dir
Loop

' now process each folder (recursion)
For Each vFolderName In colFolders
Call FillDir(startDir & vFolderName & "\", dlist)
Next vFolderName

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top