+ Reply to Thread
Results 1 to 3 of 3

Searching folders for list of files

Hybrid View

  1. #1
    Registered User
    Join Date
    01-02-2014
    Location
    Mississippi
    MS-Off Ver
    Excel 2010
    Posts
    2

    Searching folders for list of files

    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.
    Attached Images Attached Images
    Last edited by joltremari; 01-02-2014 at 05:44 PM. Reason: Wrong image attached

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Searching folders for list of files

    Just use UCase.
        For Each Myfile In MySource.Files
            If Right(UCase(Myfile.Name, 4)) = ".DWG" Then
                For Gloop = MyInt1 To MyInt2
    
                    If UCase(Myfile.Name) = UCase(Cells(Gloop, 10).Value & Cells(Gloop, 11) & ".DWG") Then
                        iCol = 2
                        Cells(iRow, iCol).Value = UCase(Myfile.Path)
                        iCol = iCol + 1
                        Cells(iRow, iCol).Value = UCase(Myfile.Name)
                        iRow = iRow + 1
                    End If
    
                Next Gloop
            End If
        Next Myfile
    David
    (*) Reputation points appreciated.

  3. #3
    Registered User
    Join Date
    01-02-2014
    Location
    Mississippi
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Searching folders for list of files

    Thank you, that works great. Just had to change one thing:

    If Right(UCase(Myfile.Name, 4)) = ".DWG" Then
    
    'Changed to:
    
    If UCase(Right(Myfile.Name, 4)) = ".DWG" Then

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA code for searching file through folders and sub folders
    By vikas189 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-05-2012, 06:00 PM
  2. Replies: 0
    Last Post: 07-13-2012, 10:51 PM
  3. List files in folders in excel
    By jayblack in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-21-2010, 07:25 AM
  4. Map/List of folders, subfolders & files
    By Bogdan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-11-2006, 01:10 PM
  5. [SOLVED] Can anyone help me Create Excel list of files in windows folders
    By solrac1956 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-28-2005, 07:10 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1