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

Try

Sub StartHere()
Dim FF As Object
Dim FSO As Object
Dim Rng As Range
Set Rng = Range("a1") '<< CHANGE IF REQUIRED
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FF = FSO.GetFolder("C:") ' << CHANGE IF REQUIRED

DoFolder FF, Rng
End Sub

Sub DoFolder(F As Scripting.Folder, Rng As Range)
Dim FF As Object
Rng.Value = F.Path
Set Rng = Rng(2, 1)
For Each FF In F.SubFolders
DoFolder FF, Rng
Next FF
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com

"greasybeano" wrote in message
...
chip thanks for you kind assistance - code good but want print
a list all the
directories associated with each top level directory - sorry if
did not make
that clear.

regards
--
GB


"Chip Pearson" wrote:

If all you need is the top subdirectories, not all
directories,
use


Dim FSO As Object
Dim Fldr As Object
Dim SubFldr As Object
Dim Rng As Range
Set Rng = Range("A1")

Set FSO = CreateObject("Scripting.FileSystemObject")
Set Fldr = FSO.getfolder("C:\")
For Each SubFldr In Fldr.subfolders
Rng.Value = SubFldr.Path
Set Rng = Rng(2, 1)
Next SubFldr



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"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