Hello,
I am working with this code snippet. I am very happy with it but I need one small tweak. When I run this code it is going through over 5,000 files and many different sub folders but I really only need it to search for specific file types (mainly Solidworks files .SDDRW & SLDDRW). How would I go about being more specific with my search?
I did manage to stumble on to this code which seems to narrow down my search to a specific file but I wouldn't really know how to combine the two.
Any help is greatly appreciated.
Thank you in advance!
Sub ProcessAll(sPath As String)
Dim Wb As Workbook, sFile As String
sFile = Dir(sPath & "*.xls")
'Loop through all .xls-Files in that path
Do While sFile <> ""
Set Wb = Workbooks.Open(sPath & sFile)
'Do something with that Workbook, insert whatever you want to do here
Debug.Print Wb.Name
'You can save it, if you like, here it's not saved
Wb.Close False
sFile = Dir
Loop
End Sub
MAIN CODE
Sub ListFiles()
'Set a reference to Microsoft Scripting Runtime by using
'Tools > References in the Visual Basic Editor (Alt+F11)
'Declare the variables
Dim objFSO As Scripting.FileSystemObject
Dim objTopFolder As Scripting.Folder
Dim strTopFolderName As String
'Insert the headers for Columns A through B
Range("A1").Value = "File Name"
Range("F1").Value = "Path"
'Assign the top folder to a variable
strTopFolderName = "C:\Users\Domenic\Documents"
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the top folder
Set objTopFolder = objFSO.GetFolder(strTopFolderName)
'Call the RecursiveFolder routine
Call RecursiveFolder(objTopFolder, True)
'Change the width of the columns to achieve the best fit
Columns.AutoFit
End Sub
Sub RecursiveFolder(objFolder As Scripting.Folder, _
IncludeSubFolders As Boolean)
'Declare the variables
Dim objFile As Scripting.File
Dim objSubFolder As Scripting.Folder
Dim NextRow As Long
'Find the next available row
NextRow = Cells(Rows.Count, "A").End(xlUp).Row + 1
'Loop through each file in the folder
For Each objFile In objFolder.Files
Cells(NextRow, "A").Value = objFile.Name
Cells(NextRow, "B").Value = objFile.Path
NextRow = NextRow + 1
Next objFile
'Loop through files in the subfolders
If IncludeSubFolders Then
For Each objSubFolder In objFolder.SubFolders
Call RecursiveFolder(objSubFolder, True)
Next objSubFolder
End If
End Sub
Bookmarks