Results 1 to 20 of 125

Thread: Combine recursive listing with excluded code

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,888
    Location
    I'm still not sure about some things that you're doing, but I still had my test folder tree so I took another look

    This seems to work

    IMHO it's better to modularize and have subs/functions that can be used and which only perform one function

    So there's Init(), ListInfor(), etc.

    You can make the code more elegant, but I sort of brute forced a lot of it.

    I also deleted your "\\?" since I didn't need it

    Option Explicit
    
    
    Const sPathTop As String = "D:\Test"
    
    
    Dim aryExclude As Variant
    Dim rowOut As Long
    Dim oFSO As Object
    Dim wsOut As Worksheet
    
    
    Sub Start()
        aryExclude = Array( _
            "D:\Test\111\111CCC\111CCC111", _
            "D:\Test\333\333AAA", _
            "D:\Test\333\333BBB" _
            )
        
        Init
    
    
        Call GetFiles(oFSO.GetFolder(sPathTop))
        
        wsOut.Columns(5).NumberFormat = "m/dd/yyyy"
        wsOut.Columns(6).NumberFormat = "m/dd/yyyy"
        wsOut.Columns(7).NumberFormat = "#,##0,.0 ""KB"""
    
    
        MsgBox "Done"
    End Sub
    
    
    
    
    
    
    Sub GetFiles(oPath As Object)
        Dim oFolder As Object, oSubFolder As Object, oFile As Object
    
    
        If IsExcluded(oPath) Then Exit Sub  '   stops recursion
            
        Call ListInfo(oPath, "Folder")
        
        For Each oFile In oPath.Files
            Call ListInfo(oFile, "File")
        Next
        
        For Each oSubFolder In oPath.SubFolders
            Call GetFiles(oSubFolder)
        Next
    End Sub
    
    
    '============================================================================
    Private Sub Init()
        Set wsOut = Worksheets("Files")
        
        rowOut = 1
        
        With wsOut
            .Cells(rowOut, 1).Value = "FILE/FOLDER PATH"
            .Cells(rowOut, 2).Value = "PARENT FOLDER"
            .Cells(rowOut, 3).Value = "FILE/FOLDER NAME"
            .Cells(rowOut, 4).Value = "FILE or FOLDER"
            .Cells(rowOut, 5).Value = "DATE CREATED"
            .Cells(rowOut, 6).Value = "DATE MODIFIED"
            .Cells(rowOut, 7).Value = "SIZE"
            .Cells(rowOut, 8).Value = "TYPE"
        End With
    
    
        rowOut = rowOut + 1
    
    
        Set oFSO = CreateObject("Scripting.FileSystemObject")
    End Sub
    
    
    '   IFolder object
    '       Attributes, DateCreated, DateLastAccessed, DateLastModified, Drive,
    '       Files, IsRootFolder, Name, ParentFolder (IFolder), Path,
    '       ShortName, ShortPath, Size, SubFolders, Type
    
    
    '   iFile object
    '       Attributes, DateCreated, DateLastAccessed, DateLastModified, Drive (IDrive),
    '       Name, ParentFolder (IFolder), Path, ShortName, ShortPath, Size, Type
    '       Attributes
    
    
    Private Sub ListInfo(oFile As Object, sType As String)
        With oFile
            wsOut.Cells(rowOut, 1).Value = .path
            wsOut.Cells(rowOut, 2).Value = .ParentFolder.path
            wsOut.Cells(rowOut, 3).Value = .Name
            wsOut.Cells(rowOut, 4).Value = sType
            wsOut.Cells(rowOut, 5).Value = .DateCreated
            wsOut.Cells(rowOut, 6).Value = .DateLastModified
            wsOut.Cells(rowOut, 7).Value = .Size
            wsOut.Cells(rowOut, 8).Value = .Type
        End With
        
        rowOut = rowOut + 1
    End Sub
    
    
    
    
    Private Function IsExcluded(p As Object) As Boolean
        Dim i As Long
        
        IsExcluded = True
        
        For i = LBound(aryExclude) To UBound(aryExclude)
            If UCase(p.path) = UCase(aryExclude(i)) Then Exit Function  '   <<<<<<<
        Next i
        
        IsExcluded = False
    End Function
    Attached Files Attached Files
    Last edited by Paul_Hossler; 02-15-2021 at 08:26 PM. Reason: Modularized and added eye candy to the macro
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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