listing directories in the Immediate Pane

P

Paul

I've used the following code to list the files in a specified directory, but
I would like to modify it so it would list the subdirectories in that
location instead of the files.


Sub testDir2(strpath As String)
Dim strFile As String
If Right(strpath, 1) <> "\" Then strpath = strpath & "\"
strFile = Dir(strpath)
Do Until Len(strFile) = 0
' do something with strFile
Debug.Print strFile
' get next file
strFile = Dir
Loop
End Sub


How can I modify that code to list the directories instead of the files?

Thanks in advance,

Paul
 
J

J_Goddard via AccessMonster.com

Hi-

This code is extracted from the A2000 help:

' 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

It can easily be modified to suit your requirements.

John
 
P

Paul

Thanks, John. I tweaked it a bit for my own use, and it works fine.

Here is what I'm using:

Sub ListDirectories(strPath As String)
Dim MyName As String
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
MyName = Dir(strPath, 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(strPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' Display directories only
End If
End If
MyName = Dir ' Get next entry.
Loop
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