List files in a directory tree

A

Allen H

I have a simple routine that I modified from a posting by Andy Gallagher to
list all the files in a single folder in a word document.

I need a routine to list all the files in a folder and all its' subfolders.

Any suggestions would be appreciated.

Sub DirList()
'
Dim directoryname As String
'browse for folder
directoryname = GetFolderName("Choose a folder")
Set fso = CreateObject("Scripting.FileSystemObject")
Set mainfolder = fso.GetFolder(directoryname)
Set filecollection = mainfolder.Files
For Each file In filecollection
Selection.Text = file.Name
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeParagraph
Next
End Sub
_______________________________________

Function GetFolderName(sCaption As String) As String
'Needs a reference to (Tools > Reference)
'Microsoft Shell Controls And Automation
Dim oShell As Shell32.Shell
Dim oFolder As Shell32.Folder
Dim oItems As Shell32.FolderItems
Dim Item As Shell32.FolderItem

On Error GoTo CleanUp

Set oShell = New Shell
Set oFolder = oShell.BrowseForFolder(0, sCaption, 0)
Set oItems = oFolder.Items
Set Item = oItems.Item

GetFolderName = Item.Path

CleanUp:
Set oShell = Nothing
Set oFolder = Nothing
Set oItems = Nothing
Set Item = Nothing

End Function
 
J

Jezebel

The way to do this is to use a recursive function that takes a folder name
as argument -- that function lists all files in the folder, then calls
itself with each of its sub-folders:

Sub ListFiles(FSO as Scripting.FileSystemObject, FolderName as string)

Dim pFile as Scripting.File
Dim pFolder as Scripting.Folder

with FSO.GetFolder(FolderName)
For each pFile in .Files
... insert pFile.Name
Next
For each pFolder in .Folders
ListFiles FSO, pFolder.Name
Next
end with

End Sub


Start the thing running by creating your FSO object and getting the root
folder --

Sub DirList()

Dim pFSO as scripting.FileSystemObject

Set pFSO = New Scripting.FileSystemObject
ListFiles pFSO, GetFolder

End Sub
 
A

Allen H

Thanks for the response.

I don't know enough of the VBA object model to really understand what is
occuring. It certainly looks much simpler than I thought it would be.

When I first tried to run it I got a compile error on "FSO as
Scripting.FileSystemObject". I quessed that a reference was missing and when
I added a reference to the Microsoft Scripting Runtime the error disappeared.

Now there is a "ByRef argument type mismatch" compile error on "Getfolder".
At this time I'm really guessing and may be going in the wrong direction but
I think "Getfolder" should be "Getfoldername" so it returns a string.

If that is correct then the next error is a compile error on ".Folders".
From what I can see ".Folders" is not set to anything.

I can continue to stumble blindly, but any additional quidance would be
appreciated.
 
J

Jezebel

Sorry, sloppy aircode. The second loop should be

For Each pFolder In .SubFolders
ListFiles FSO, pFolder.Path
Next


The "GetFolder" in the DirList function was meant as a call to YOUR
function, that browses for a folder and returns a string. You could insert a
variable or a literal string here instead.
 

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