Hello, for my first post I would like a little help with searching a directory tree for a list of files.
Here's what I have - A boat load of directories, thousands of cad drawings, and a poor naming convention.
As I put this together I realized I needed to keep adding conditions to search by: some drawings are named such as 'A12345.dwg' some may be opposite: '12345A.dwg'. Also, some file names have the extension upper case and some do not; (.dwg or .DWG) This makes for a lot of testing to find the files. If someone can help me out with making this faster or just "better" in general I would appreciate it. I am a self taught in VBA so go easy on me for any crudity in my coding... Thanks!
Sub ListMyFiles(mySourcePath, IncludeSubfolders)
Set MyObject = New Scripting.FileSystemObject
Set MySource = MyObject.GetFolder(mySourcePath)
Dim Gloop As Integer
Dim MyInt1 As Integer
Dim MyInt2 As Integer
MyInt1 = 2
MyInt2 = Cells(1, 12)
On Error Resume Next
For Each Myfile In MySource.Files
If InStr(Myfile.Name, "dwg") <> 0 Or InStr(Myfile.Name, "DWG") <> 0 Then
For Gloop = MyInt1 To MyInt2
If Myfile.Name = Cells(Gloop, 10).Value & Cells(Gloop, 11) & ".dwg" Or Myfile.Name = Cells(Gloop, 10).Value & Cells(Gloop, 11) & ".DWG" Then
iCol = 2
Cells(iRow, iCol).Value = Myfile.Path
iCol = iCol + 1
Cells(iRow, iCol).Value = Myfile.Name
iRow = iRow + 1
Else
If Myfile.Name = Cells(Gloop, 11).Value & Cells(Gloop, 10) & ".dwg" Or Myfile.Name = Cells(Gloop, 11).Value & Cells(Gloop, 10) & ".DWG" Then
iCol = 2
Cells(iRow, iCol).Value = Myfile.Path
iCol = iCol + 1
Cells(iRow, iCol).Value = Myfile.Name
iRow = iRow + 1
End If
End If
Next Gloop
End If
Next Myfile
If IncludeSubfolders Then
For Each mySubFolder In MySource.SubFolders
Call ListMyFiles(mySubFolder.Path, True)
Next
End If
End Sub
SS1.png
The image above may help understand the code. I am pasting the list into J & K columns, J holds letters, K holds numbers.
As results are found, the full path is listed starting at B11, and just the file name starting at C11.
Bookmarks