alright well here's some code that i used. you have to set a reference to VBScript in order to make it work: Hit Tools > References then scroll down and put a check next to Microsoft Scripting Runtime.
I have it set up as a class module, so what you'll need to do is right-click on the project explorer, then click 'Add class module', then name it "clsFileHandler". In your new class module, copy the following code:
Code:
'To use this class module in another workbook, you'll need to set a reference to the "microsoft scripting runtime" library
Option Explicit
Private FSO As Scripting.FileSystemObject
'For putting the file list into a worksheet
Private lTargetOffset As Long
Public Sub GetAllFiles(FolderPath As String, Target As Range) 'including those in sub folders
'gets all files in FolderPath, including those in subfolders
Static oParentFolder As Scripting.Folder
Static oSubFolder As Scripting.Folder
Static oFolders As Scripting.Folders
Set oParentFolder = FSO.GetFolder(FolderPath)
Set oFolders = oParentFolder.SubFolders
GetFileList FolderPath, Target
If oFolders.Count = 0 Then 'there's no subfolders in this folder
Exit Sub
End If
For Each oSubFolder In oFolders
GetAllFiles oSubFolder.Path, Target
Next
End Sub
Public Sub ClearPreviousSearch(shtSheet As Worksheet)
shtSheet.UsedRange.Offset(1, 0).EntireRow.Delete
lTargetOffset = 0
End Sub
Public Sub GetFileList(FolderToSearch As String, Target As Range) 'in current folder only, no sub folders
'this will put a list of files on a spreadsheet at the Target, does not include files in subfolders
Static oMainFolder As Scripting.Folder
Static oFileCollection As Scripting.Files
Static oFile As Scripting.File
'Put a trailing backslash on path if it's not there
If Right(FolderToSearch, 1) <> "\" Then
FolderToSearch = FolderToSearch & "\"
End If
Set oMainFolder = FSO.GetFolder(FolderToSearch)
Set oFileCollection = oMainFolder.Files
For Each oFile In oFileCollection
' 'here's a few other properties you can return
' oFile.Path
' oFile.DateCreated
' oFile.DateLastModified
' 'and a method
' oFile.Delete
Target.Offset(lTargetOffset, 0) = oFile.Path
Target.Offset(lTargetOffset, 1) = oFile.Name
lTargetOffset = lTargetOffset + 1
Next oFile
End Sub
Private Sub Class_Initialize()
Set FSO = New Scripting.FileSystemObject
End Sub
Private Sub Class_Terminate()
Set FSO = Nothing
End Sub
Then in your regular module, the one you're putting code into, put the following:
Code:
Dim FH As New clsFileHandler
FH.ClearPreviousSearch shtFiles
FH.GetAllFiles "C:\Users\Mike\Documents\Business\StatsCustomers1\CustomerWebsites\elizabethsloomroom", shtFiles.Range("A2")
Set FH = Nothing
Next, rename an extra worksheet as shtFiles. Now, you need to be careful here. You need to rename it in the VBE. Click on the sheet in the project explorer, then in the properties window at the top, you'll see "(Name)", do not confuse this with "Name" without the parantheses. Then change the folder path to whatever you want and test it