Oct 24, 2014

Get File Names from Child Directory

Sub GetFilesFromDirectory()
Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object
Dim i As Integer
 
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("C:\Documents\")
i = 1
'loops through each file in the directory and prints their names and path
For Each objSubFolder In objFolder.subfolders
    'loops though each child directory
    For Each objSecSubFolder In objSubFolder.subfolders
       'loops though each file
        For Each objFile In objSecSubFolder.Files
            'print file name
            Cells(i + 1, 1) = objFile.Name
            'print file path
            Cells(i + 1, 2) = objFile.Path
            i = i + 1
        Next objFile
    Next objSecSubFolder
Next objSubFolder

End Sub

No comments:

Search