+ Reply to Thread
Results 1 to 8 of 8

List folders without using FSO

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Post List folders without using FSO

    I am trying to return an array of folder names without using FSO. Is there anyway Dir can be used to return folders only?

    Below is code I am trying to alter to return folder names

    Option Explicit
    
    Public Function fnavarGet1dArrFiles_ByDir(ByVal strStartFolder As String, _
                                                Optional ByVal strFileFilter As String = "*", _
                                                Optional ByVal blnReturnFullName As Boolean _
                                                ) As Variant
    
    '// Returns an array of file names that match strFileFilter
    '// Use argument to choose whether you want FullNames or FileNames only
    '// If no matching files are found, it returns Empty
    
    '// Credits: adapted from function by John Walkenbach
    
    '// Note: The strFileFilter is NOT case sensitive
    '// Dir method does not detect hidden files
    
        Dim avarFileArray() As Variant
        Dim lngFileCount    As Long
        Dim strFileName     As String
    
        On Error GoTo ErrHandler
    
        strStartFolder = fnstrGetSeparatoredPath(strStartFolder)
    
        If Not fnblnExistsFileFolder(strStartFolder) Then
            GoTo NoFilesFound
        End If
    
        'If no files in folder, exit the sub
        strFileName = Dir(strStartFolder & strFileFilter)
        If Len(strFileName) = 0 Then
            GoTo NoFilesFound
        End If
    
        'Loop until no more matching files are found
        If blnReturnFullName Then
            Do While Len(strFileName)
                lngFileCount = lngFileCount + 1
                ReDim Preserve avarFileArray(1 To lngFileCount)
                avarFileArray(lngFileCount) = strStartFolder & strFileName
                strFileName = Dir()
            Loop
        Else
            Do While Len(strFileName)
                lngFileCount = lngFileCount + 1
                ReDim Preserve avarFileArray(1 To lngFileCount)
                avarFileArray(lngFileCount) = strFileName
                strFileName = Dir()
            Loop
        End If
    
        fnavarGet1dArrFiles_ByDir = avarFileArray
    Exit Function
    
    ErrHandler:
        fnavarGet1dArrFiles_ByDir = Empty
    Exit Function
    
    NoFilesFound:
        fnavarGet1dArrFiles_ByDir = Empty
    End Function
    
    Public Function fnblnExistsFileFolder(ByVal strFullName As String) As Boolean
    'adapted from function written by Ken Puls (www.excelguru.ca)
        If Len(strFullName) Then
            On Error Resume Next
            fnblnExistsFileFolder = Len(Dir(strFullName, 31))
            On Error GoTo 0
        End If
    End Function
    
    Public Function fnstrGetSeparatoredPath(ByRef strPath As String, Optional ByVal blnInvert As Boolean) As String
    '/ ensures folder path ends in path separator (aka trailing backslash)
    '/ doesn't detect garbage input, requires a Path arg
        Dim blnChange As Boolean
        Const strcPATH_SEPARATOR As String = "\"
    
        If Len(strPath) Then
            blnChange = ((Right$(strPath, 1) = strcPATH_SEPARATOR) = blnInvert)
            If Not blnChange Then
                fnstrGetSeparatoredPath = strPath
            ElseIf Not blnInvert Then
                'add trailing separator
                fnstrGetSeparatoredPath = strPath & strcPATH_SEPARATOR
            Else
                'remove last character
                fnstrGetSeparatoredPath = Left$(strPath, Len(strPath) - 1)
            End If
        End If
    End Function
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: List folders without using FSO

    You can And the attributes (via GetAttr) with vbDirectory to see if it's a file or folder.
    Last edited by shg; 08-01-2016 at 07:44 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: List folders without using FSO

    Quote Originally Posted by shg View Post
    You can And the attributes (via GetAttr) with vbDirectory to see if it's a file or folder.
    Thanks shg. When I use get attributes and vbDirectory, it returns a few files in addition to the folders.

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,272

    Re: List folders without using FSO

    Try this.
    Sub ListDirectories()
        Dim sDirName As String, sPath As String
        sPath = "C:\"
        sDirName = Dir(sPath, vbDirectory)
        Do Until Len(sDirName) = 0
            'only list folders
           If GetAttr(sPath & sDirName) And vbDirectory Then
                Debug.Print sDirName
            End If
            sDirName = Dir 'get next file
       Loop
    End Sub
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: List folders without using FSO

    Thanks bakerman! (Reps) It works.


    For anyone who found on this thread while searching for a solution, the finished code is below:

    Public Function fnavarGet1dArrFolders_ByDir(ByVal strStartFolder As String, _
                                                Optional ByVal strFolderFilter As String = "*", _
                                                Optional ByVal blnReturnFullName As Boolean _
                                                ) As Variant
    
    '// Returns an array of folder names that match strFolderFilter
    '// Use argument to choose whether you want full paths or FolderNames only
    '// If no matching folders are found, it returns Empty
    
    '// Credits: adapted from function by John Walkenbach
    
    '// Note: The strFolderFilter is NOT case sensitive
    
        Dim avarFolderArray() As Variant
        Dim lngFolderCount    As Long
        Dim strFolderName     As String
    
        On Error GoTo ErrHandler
    
        strStartFolder = fnstrGetSeparatoredPath(strStartFolder)
    
        If Not fnblnExistsFileFolder(strStartFolder) Then
            GoTo NoFoldersFound
        End If
    
        'If no folders in folder, exit the sub
        strFolderName = Dir(strStartFolder & strFolderFilter, vbDirectory)
        If Len(strFolderName) = 0 Then
            GoTo NoFoldersFound
        End If
    
        'Loop until no more matching folders are found
    '(separate loops for marginal speed improvement)
        If blnReturnFullName Then
            Do Until Len(strFolderName) = 0
                'only list folders
                If GetAttr(strStartFolder & strFolderName) And vbDirectory Then
                    lngFolderCount = lngFolderCount + 1
                    ReDim Preserve avarFolderArray(1 To lngFolderCount)
                    avarFolderArray(lngFolderCount) = strStartFolder & strFolderName
                End If
                strFolderName = Dir()
            Loop
        Else
            Do Until Len(strFolderName) = 0
                'only list folders
                If GetAttr(strStartFolder & strFolderName) And vbDirectory Then
                    lngFolderCount = lngFolderCount + 1
                    ReDim Preserve avarFolderArray(1 To lngFolderCount)
                    avarFolderArray(lngFolderCount) = strFolderName
                End If
                strFolderName = Dir()
            Loop
        End If
    
        fnavarGet1dArrFolders_ByDir = avarFolderArray
    Exit Function
    
    ErrHandler:
        fnavarGet1dArrFolders_ByDir = Empty
    Exit Function
    
    NoFoldersFound:
        fnavarGet1dArrFolders_ByDir = Empty
    End Function
    
    Private Function fnblnExistsFileFolder(ByVal strFullName As String) As Boolean
    'adapted from function written by Ken Puls (www.excelguru.ca)
        If Len(strFullName) Then
            On Error Resume Next
            fnblnExistsFileFolder = Len(Dir(strFullName, 31))
            On Error GoTo 0
        End If
    End Function
    
    Private Function fnstrGetSeparatoredPath(ByRef strPath As String, Optional ByVal blnInvert As Boolean) As String
    '/ ensures folder path ends in path separator (aka trailing backslash)
    '/ doesn't detect garbage input, requires a Path arg
        Dim blnChange As Boolean
        Const strcPATH_SEPARATOR As String = "\"
    
        If Len(strPath) Then
            blnChange = ((Right$(strPath, 1) = strcPATH_SEPARATOR) = blnInvert)
            If Not blnChange Then
                fnstrGetSeparatoredPath = strPath
            ElseIf Not blnInvert Then
                'add trailing separator
                fnstrGetSeparatoredPath = strPath & strcPATH_SEPARATOR
            Else
                'remove last character
                fnstrGetSeparatoredPath = Left$(strPath, Len(strPath) - 1)
            End If
        End If
    End Function
    Last edited by mc84excel; 08-09-2016 at 06:27 PM.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: List folders without using FSO

    @b2: Thank you.

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,272

    Re: List folders without using FSO

    @shg: You're welcome and many thanks for rep points.

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,272

    Re: List folders without using FSO

    @ mc84excel

    You're welcome and thanks for the rep points.

+ 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. [SOLVED] Create folders and Sub folders and Sub-Sub folders from 5 columns with VBA
    By arleutwyler in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-11-2014, 04:16 PM
  2. [SOLVED] Need folders copies into other folders based on excel list
    By swmatrixman in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-19-2013, 02:08 PM
  3. Replies: 0
    Last Post: 12-04-2012, 01:01 PM
  4. List of Folders
    By toocold in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-01-2008, 10:28 PM
  5. Make folders from List
    By Vlad999 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-25-2007, 01:41 AM
  6. Creating List From Folders
    By Don in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-10-2006, 07:50 PM
  7. List of Folders
    By marianne in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-07-2006, 06:55 AM
  8. List of folders in a certain directory
    By woolyhedgehog in forum Excel General
    Replies: 1
    Last Post: 10-19-2005, 12:05 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