Getting names of files in subdirectories

D

David

The following procedures will loop through a specified directory and
pull all the file names. Is there a way to adjust this code to loop
through a directory and all of it's subdirectories and pull the file
names and paths.

TIA

David

Sub Get_File_Names(SearchDir As String)
Dim FoundFiles
Dim FileName As String
Index = 1
FoundFiles = DirList(SearchDir)
End Sub

Function DirList(Indir As String)
Dim FileMatrix(1 To 500)
On Error Resume Next
ListFile = Dir(Indir)
'
Index = 1
Do While Not ListFile = ""
FileMatrix(Index) = ListFile
Range("SourceFiles1").Rows(Index).Value = Indir & ListFile
Index = Index + 1
ListFile = Dir()
Loop
End Function
 
J

Joel

Dim RowNumber
Sub GetFolderSize()

strFolder = "C:"
RowNumber = 1

Set fso = CreateObject _
("Scripting.FileSystemObject")
Set folder = _
fso.GetFolder(strFolder)

Sheets(1).Cells(RowNumber, 1) = strFolder + "\"
Sheets(1).Cells(RowNumber, 2) = folder.Size
RowNumber = RowNumber + RowNumber

Call GetSubFolder(strFolder + "\")
End Sub

Sub GetSubFolder(strFolder)
Set fso = CreateObject _
("Scripting.FileSystemObject")

Set folder = _
fso.GetFolder(strFolder)

If folder.subfolders.Count > 0 Then
For Each sf In folder.subfolders
On Error GoTo 100
Call GetSubFolder(strFolder + sf.Name + "\")
100 Next sf
End If
'folder size in bytes
On Error GoTo 200
For Each fl In folder.Files
Sheets(1).Cells(RowNumber, 3) = fl.DateLastModified
Sheets(1).Cells(RowNumber, 2) = fl.Size
Sheets(1).Cells(RowNumber, 1) = strFolder & fl.Name
RowNumber = RowNumber + 1
Next fl

200 On Error GoTo 0

End Sub
 

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