View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.misc
Jim Cone Jim Cone is offline
external usenet poster
 
Posts: 3,290
Default Listing the contents of a folder


Change strPath to the desired folder.
Listing added to Column B of active sheet.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware

Sub ListAllFilesInFolder()
'Jim Cone - San Francisco - Oct 2006
Dim strPath As String
Dim oFSO As Object
Dim oFile As Object
Dim oFolder As Object

Dim N As Long
N = 1
strPath = "C:\Documents and Settings\Name\My Documents\Excel Files"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(strPath)
Cells(N, 1).Value = oFolder.Path
N = N + 1
For Each oFile In oFolder.Files
Cells(N, 2).Value = oFile.Name
N = N + 1
Next 'oFile
Call ListSubFolderFiles(oFolder, N)

Set oFSO = Nothing
Set oFile = Nothing
Set oFolder = Nothing
End Sub

Function ListSubFolderFiles(ByRef oParentFolder As Object, ByRef lngR As Long)
Dim oSubFolder As Object
Dim oFile As Object
For Each oSubFolder In oParentFolder.SubFolders
Cells(lngR, 1).Value = oSubFolder.Path
Range(Cells(lngR, 1), Cells(lngR, 5)).Interior.ColorIndex = 15
lngR = lngR + 1
For Each oFile In oSubFolder.Files
Cells(lngR, 2).Value = oFile.Name
lngR = lngR + 1
Next
ListSubFolderFiles oSubFolder, lngR
Next 'oSubFolder
End Function
'---------------



wrote in message
I am looking to write a macro that will list the contents of a
specified folder, including the contents of subfolders. Is this
something that i can do with a macro?
Thanks.
Asa