+ Reply to Thread
Results 1 to 7 of 7

List files sub-folders - selective

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-22-2022
    Location
    UK/Australia
    MS-Off Ver
    365
    Posts
    137

    List files sub-folders - selective

    I know there are a few ways to list files in a selected folder and all it's sub-folders (see below for example).

    Can you selectively process folders?
    In my case:
    - Main folder is chosen.
    - Level 2. Look only in selected folders (yellow colour). Don't list files though.
    - Level 3. Look in all folders (green colour). List all files in each folder.
    - Level 4+. No.

    I don't know how to tell the macro to list files only in Level 3.

    Untitled.png


    Sub MainList()
        Set Folder = Application.FileDialog(msoFileDialogFolderPicker)
        If Folder.Show <> -1 Then Exit Sub
        xDir = Folder.SelectedItems(1)
        Call ListFilesInFolder(xDir, True)
    End Sub
    
    Sub ListFilesInFolder(ByVal xFolderName As String, ByVal xIsSubfolders As Boolean)
        Dim xFileSystemObject As Object
        Dim xFolder As Object
        Dim xSubFolder As Object
        Dim xFile As Object
        Dim rowIndex As Long
        Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
        Set xFolder = xFileSystemObject.GetFolder(xFolderName)
        rowIndex = Application.ActiveSheet.Range("A65536").End(xlUp).Row + 1
        For Each xFile In xFolder.Files
          Application.ActiveSheet.Cells(rowIndex, 1).Formula = xFile.Name
          rowIndex = rowIndex + 1
        Next xFile
        If xIsSubfolders Then
          For Each xSubFolder In xFolder.SubFolders
            ListFilesInFolder xSubFolder.Path, True
          Next xSubFolder
        End If
        Set xFile = Nothing
        Set xFolder = Nothing
        Set xFileSystemObject = Nothing
    End Sub

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: List files sub-folders - selective

    You could add an extra parameter to the ListFilesInFolder subroutine which is an integer which shows the current level which is being trawled. Logic inside the subroutine could work off this parameter. When calling the subroutine, the parameter would be set to one greater than the value of that parameter in the calling instance.

  3. #3
    Forum Contributor
    Join Date
    01-22-2022
    Location
    UK/Australia
    MS-Off Ver
    365
    Posts
    137

    Re: List files sub-folders - selective

    Thanks I'll try that.
    Would it stop the macro looking at folders past that level though?

  4. #4
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: List files sub-folders - selective

    Something like this:

    Sub MainList()
        Set Folder = Application.FileDialog(msoFileDialogFolderPicker)
        If Folder.Show <> -1 Then Exit Sub
        xDir = Folder.SelectedItems(1)
        Call ListFilesInFolder(xDir, True, 1, "|3|")
    End Sub
    Sub ListFilesInFolder(ByVal xFolderName As String, ByVal xIsSubfolders As Boolean, xCurrentLevel As Long, xMatchLevel As String)
        Dim xFileSystemObject As Object
        Dim xFolder As Object
        Dim xSubFolder As Object
        Dim xFile As Object
        Dim rowIndex As Long
        Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
        Set xFolder = xFileSystemObject.GetFolder(xFolderName)
        rowIndex = Application.ActiveSheet.Range("A65536").End(xlUp).Row + 1
        If InStr(1, xMatchLevel, "|" & CStr(xCurrentLevel) & "|") > 0 Then
          For Each xFile In xFolder.Files
            Application.ActiveSheet.Cells(rowIndex, 1).Formula = xFile.Name
            rowIndex = rowIndex + 1
          Next xFile
        End If
        If xIsSubfolders Then
          For Each xSubFolder In xFolder.SubFolders
            ListFilesInFolder xSubFolder.Path, True, xCurrentLevel + 1, xMatchLevel
          Next xSubFolder
        End If
        Set xFile = Nothing
        Set xFolder = Nothing
        Set xFileSystemObject = Nothing
    End Sub
    If you wanted to list files at multiple levels you can change the last parameter. E.g. levels 1, 3 and 4 would be "|1|3|4|"

    WBD
    Office 365 on Windows 11, looking for rep!

  5. #5
    Forum Contributor
    Join Date
    01-22-2022
    Location
    UK/Australia
    MS-Off Ver
    365
    Posts
    137

    Re: List files sub-folders - selective

    This is very good actually.
    Thanks for posting.

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2507 (Windows 11 Home 24H2 64-bit)
    Posts
    91,786

    Re: List files sub-folders - selective

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, if you have not already done so, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of all those who offered help.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  7. #7
    Forum Contributor
    Join Date
    01-22-2022
    Location
    UK/Australia
    MS-Off Ver
    365
    Posts
    137

    Re: List files sub-folders - selective

    Sure, no problems.

+ 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. Macro to list all files and folders in sub folders and choose a starting folder
    By LeanAccountant in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-06-2021, 02:04 PM
  2. List both files and folders in a directory
    By Un-Do Re-Do in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-11-2019, 12:18 PM
  3. Replies: 5
    Last Post: 11-02-2018, 03:15 AM
  4. List all folders/files?
    By Macmanzx2000 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-10-2015, 05:55 AM
  5. Searching folders for list of files
    By joltremari in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-03-2014, 02:09 PM
  6. List files in folders in excel
    By jayblack in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-21-2010, 07:25 AM
  7. Map/List of folders, subfolders & files
    By Bogdan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-11-2006, 01:10 PM

Tags for this Thread

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