Finding the directories or folders

R

Ray

I am attempting to run this code from the help file example for Dir
' Display the names in C:\ that represent directories.
MyPath = "c:\" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
' Ignore the current directory and the encompassing directory.
If MyName <> "." And MyName <> ".." Then
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' Display entry only if it
End If ' it represents a directory.
End If
MyName = Dir ' Get next entry.
Loop

Howerver this line
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
lookes like it is supposed to determine if the file name received is a
Directory or not
I does not identify directories as directories. It acctually skips them and
just gets file names.
then when I attempt this bit of code
' Now recursively iterate through each cached subdirectory.
For I = 1 To Count
List1.AddItem Path & D(I) ' Put name in list box.
ListSubDirs Path & D(I) & "\"
Next I
It appends the backslash to a file name and attempts to use it as a folder
name.
Then it errors and says invalid file name
Now this is out of the VBA Word help file.
I know help files are always correct.
Can you show me what I have done wrong on this?
Thanks
 
J

Jay Freedman

Hi Ray,

I don't think you've done anything wrong, but something's definitely
strange. The Help example is correct (although I wouldn't go so far as
to say that "help files are always correct" -- it just ain't so). When
I copied the code you posted and ran it, it worked correctly too. So
it isn't the code, it's apparently the way it runs on your system.

Try this version on your copy of Word, and look at the value of the
Result variable printed in the Debug window. The meanings of the
numeric values are given in the Help topics on the Dir and GetAttr
functions.

Sub foo()
Dim MyPath As String, MyName As String
Dim Result As Long

' Display the names in C:\ that represent directories.
MyPath = "c:\" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
' Ignore the current directory and the encompassing directory.
If MyName <> "." And MyName <> ".." Then
' Use bitwise comparison to make sure MyName is a directory.
Result = (GetAttr(MyPath & MyName) And vbDirectory)
If Result = vbDirectory Then
Debug.Print MyName & " = directory"
Else
Debug.Print MyName & " Result = " & Result
End If
End If
MyName = Dir ' Get next entry.
Loop
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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