Listing the contents of a folder

  • Thread starter Thread starter asa.flynt.reynolds
  • Start date Start date
A

asa.flynt.reynolds

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
 
It would help if you mapped the folder that you want to list out to a drive.

I do this as follows:

Start -> Run -> cmd

I have multiple drives mapped. Let's say I want to get a listing of the Y
drive.

I type in Y:

I then type:

dir/s >z:textfile.txt

It will save a listing of the Y drive on my Z drive as a file named
textfile.txt

Good luck.
 
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
'---------------


<[email protected]>
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
 

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

Back
Top