Thread: Dir
View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
greasybeano greasybeano is offline
external usenet poster
 
Posts: 7
Default Dir

Bob thanks tried like your solution but still just getting 1st level directory?

Hope that I made clear what i am trying to achieve - for each top level
folder, I want to print out all associated subfolders but I having some
difficulty do this. Some solutions kindly offered by others may work but my
VB is limited where some adapting is required.

regards
--
GB


"Bob Phillips" wrote:

Try this variation on Ardus's code

Sub Finddir()
Dim FSO As Object
Dim fDir As Object
Dim fSubDir As Object
Dim i As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
Set fDir = FSO.GetFolder("C:\")
For Each fSubDir In fDir.SubFolders
i = i + 1
Cells(i, "A").Value = fSubDir.Name
Next fSubDir
End Sub



--
HTH

Bob Phillips

(replace somewhere in email address with googlemail if mailing direct)

"greasybeano" wrote in message
...
thanks for all who replied however, still not too clear what i need to do

(my
VB tad limited) I tried sample code provided by Ardus but get Error
"user-defined type not defined" Ron's solution looks the business but

again
not too sure how I would adapt it for my need.
I just need to show all the sub folders (from "C\" ) associated to each
first level directory. these folders may or may not contain any files - I
should mention i am using xl2003.

many thanks

--
GB


"Ardus Petus" wrote:

Dim fso As Scripting.FileSystemObject

Sub test()
Set fso = New Scripting.FileSystemObject
Finddir ("c:\")
End Sub

Sub Finddir(MyPath As String)
Dim fDir As Folder
Dim fSubDir As Folder
Set fDir = fso.GetFolder(MyPath)
Debug.Print fDir.Path
For Each fSubDir In fDir.SubFolders
Finddir fSubDir.Path
Next fSubDir
End Sub

HTH
--
AP

"greasybeano" a écrit dans le message

de
news: ...
I have taken the following code from the Dir help file which works ok

at
first level.
However, I need to search for all the associated subdirectories but

not
too
sure how to adapt the code. could someone be kind enough to assist

please?
Many thanks.
--
GB

Sub Finddir()
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 ' it
represents
a directory.
End If
End If
myname = Dir ' Get next entry.
Loop
End Sub