give that this thread was just created: https://www.excelforum.com/excel-pro...e-folders.html
these blocks of code should be used by people here quite a bit, I would assume. Change the inner workings of them to do what you need, as appropriate:
LOOP FILES IN A SINGLE DIRECTORY
Sub loop_files_in_single_dir()
Dim fso As Object
Dim file
Dim mainpath
mainpath = "directory_here\"
Set fso = CreateObject("scripting.filesystemobject")
For Each file In fso.getfolder(mainpath).Files
'CHANGE THIS CODE TO DO WHAT YOU NEED WITH THE FILES.
Debug.Print mainpath & file
Next file
End Sub
LOOP FILES IN ALL SUB DIRECTORIES OF A SINGLE DIRECTORY
Sub loop_files_in_subdirs_of_single_dir()
Dim fso As Object
Dim folder
Dim subfolder
Dim file
Dim oldname As String
Dim newname As String
Set fso = CreateObject("scripting.filesystemobject")
Set folder = fso.getfolder("directory_here\")
For Each subfolder In folder.subfolders
For Each file In subfolder.Files
'CHANGE THIS CODE TO DO WHAT YOU NEED.
Debug.Print file
Next file
Next subfolder
Set fso = Nothing
Set folder = Nothing
End Sub
LOOP SUB DIRECTORIES IN A SINGLE DIRECTORY (method 1)
Sub loop_subdirs_1()
Dim fso As Object
Dim mainpath
Dim folder
Dim folders As New Collection
Dim subfolder
mainpath = "directory_here\"
Set fso = CreateObject("scripting.filesystemobject")
folders.Add fso.getfolder(mainpath)
Set folder = folders(1)
For Each subfolder In folder.subfolders
'CHANGE THIS CODE TO DO WHAT YOU NEED.
Debug.Print subfolder.Name
Next subfolder
Set folder = Nothing
Set fso = Nothing
End Sub
LOOP SUB DIRECTORIES IN A SINGLE DIRECTORY (method 2)
Sub loop_subdirs_2()
Dim fso As Object
Dim folder
Dim subfolder
Dim oldname As String
Dim newname As String
Set fso = CreateObject("scripting.filesystemobject")
Set folder = fso.getfolder("directory_here\")
For Each subfolder In folder.subfolders
'CHANGE THIS CODE TO DO WHAT YOU NEED.
debug.print subfolder.Name
Next subfolder
Set fso = Nothing
Set folder = Nothing
End Sub
Bookmarks