+ Reply to Thread
Results 1 to 10 of 10

List both files and folders in a directory

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    List both files and folders in a directory

    Is it possible to list both files and folders in a directory?
    Using the "FileSystemObject" method.

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    O365 V2408
    Posts
    15,224

    Re: List both files and folders in a directory

    Something like this perhaps...
    Option Explicit
    
    Sub RenameFolder()
    Dim Path As String
    Dim Fso As Object, Folder As Object, Subfolder As Object, SubFld As Object, File As Object
    Path = ThisWorkbook.Path
    Set Fso = CreateObject("Scripting.FileSystemObject")
    Set Folder = Fso.GetFolder(Path)
    For Each Subfolder In Folder.subfolders
        Debug.Print Subfolder
        For Each SubFld In Subfolder.subfolders
            Debug.Print SubFld
            For Each File In SubFld.Files
                Debug.Print File
            Next File
        Next SubFld
    Next Subfolder
    End Sub
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: List both files and folders in a directory

    Quote Originally Posted by Un-Do Re-Do View Post
    Is it possible to list both files and folders in a directory?
    Using the "FileSystemObject" method.
    Yes but the more files & folders, the slower (than using Dir VBA function) …

  4. #4
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: List both files and folders in a directory

    @sintek, your code gives only folder or file. I needed both.
    For example, list in snapshot should be:

    New folder1
    New folder2
    New Microsoft Excel Worksheet1
    New Microsoft Word Document1


    Capture.JPG

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    O365 V2408
    Posts
    15,224

    Re: List both files and folders in a directory

    So...You do not want the files within the subfolders of the folders...
    You just want first level Folders and files...

  6. #6
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: List both files and folders in a directory

    Yes I do want subfolders but that will come later.
    The hard part seems to be listing folders together with files.

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    O365 V2408
    Posts
    15,224

    Re: List both files and folders in a directory

    Perhaps I just don't understand...
    If for example on your desktop you have 2 Folders and two documents...
    Must code show those 4 only....Not go in further

  8. #8
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: List both files and folders in a directory

    Yes, show 2 folders and 2 documents.
    Yes, go to subfolders where they exist.

  9. #9
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: List both files and folders in a directory

    Harder than I thought.

  10. #10
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    O365 V2408
    Posts
    15,224

    Re: List both files and folders in a directory

    Option Explicit
    
    Sub listfile()
    Dim path As String
    Dim vaArray(), i As Long
    Dim oFile As Object, oFSO As Object, oFolder As Object, oFiles As Object
    path = ThisWorkbook.path
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder(path & "\")
    Set oFiles = oFolder.Files
    If oFiles.Count = 0 Then Exit Sub
    ReDim vaArray(1 To oFiles.Count)
    i = 1
    For Each oFile In oFiles
      vaArray(i) = oFile.Name
      i = i + 1
    Next oFile
    Range("A1").Resize(i) = Application.Transpose(vaArray)
    End Sub

+ 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. Replies: 5
    Last Post: 11-02-2018, 03:15 AM
  2. [SOLVED] get list of folders in a directory
    By plans in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-30-2017, 05:16 PM
  3. [SOLVED] How to open all excel files within a directory of folders.
    By randomguy1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-17-2013, 01:55 AM
  4. Code to set combo box to list folders in a directory
    By Kezwick in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-08-2013, 07:07 AM
  5. Copy matching files/folders (using unique ID) from one directory to another
    By TommyK25 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-10-2013, 06:38 AM
  6. Generate a list of folders within a directory
    By TRT7G7 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-17-2010, 11:24 AM
  7. 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