Closed Thread
Results 1 to 6 of 6

Macro to list folder names within a directory

Hybrid View

  1. #1
    Registered User
    Join Date
    11-22-2009
    Location
    Newcastle,England
    MS-Off Ver
    Excel 2003
    Posts
    78

    Macro to list folder names within a directory

    Hi guys,

    Thanks for all the help recently but I am not yet done! Now i'm looking for a macro which within a sheet named "foldernamedump" will list in a column the folder names within a directory I specify.

    I have seen a couple of sample codes but I just cant seem to get them working at all so I think its best to start from scratch and the work i get supplied here is always perfect.

    I also want the macro to clear the contents of the sheet before it loads again just to ensure there is no old data within the sheet.

    Thank you for any help
    Last edited by munkee; 11-26-2009 at 04:04 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to list folder names within a directory

    Hello munkee,

    This macro will all the folders in a given directory. The list is output to the "Sheet1" starting in cell "A2". The folder name, path, and size are printed in the same row. You can change the directory, worksheet, and starting cell. They are all marked in red.
    Sub ListFoldersAndInfo()
    
      Dim FSO As Object
      Dim Folder As Object
      Dim FolderName As String
      Dim R As Long
      Dim Rng As Range
      Dim SubFolder As Object
      Dim Wks As Worksheet
      
      'Parent Directory - Change this to whichever directory you want to use
        FolderName = "C:\Documents and settings\Owner\My Documents"
        
        Set Wks = Worksheets("Sheet1")
        Set Rng = Wks.Range("A2")
        Wks.UsedRange.Offset(1, 0).ClearContents
        
        Set FSO = CreateObject("Scripting.FileSystemObject")
        
          Set Folder = FSO.GetFolder(FolderName)
          R = 1
          Rng.Cells(R, 1) = Folder.Name
          Rng.Cells(R, 2) = Folder.Path
          Rng.Cells(R, 3) = Folder.Size
          
            For Each Folder In Folder.SubFolders
              R = R + 1
              Rng.Cells(R, 1) = Folder.Name
              Rng.Cells(R, 2) = Folder.Path
              Rng.Cells(R, 3) = Folder.Size
            Next Folder
            
        Set FSO = Nothing
             
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

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

    Re: Macro to list folder names within a directory

    Try this

    Sub ListFolders()
    Dim fso
    Sheets("foldernamedump").Cells.Clear
    Set fso = CreateObject("Scripting.FileSystemObject")
    Root = Application.InputBox("Please enter path")
    If Right(Root, 1) <> "\" Then Root = Root & "\"
    Set Folder = fso.getfolder(Root)
    Set SubFolders = Folder.SubFolders
    For Each SubFolder In SubFolders
        Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = SubFolder.Name
    Next SubFolder
    Set fso = Nothing
    End Sub
    Martin

  4. #4
    Registered User
    Join Date
    11-22-2009
    Location
    Newcastle,England
    MS-Off Ver
    Excel 2003
    Posts
    78

    Re: Macro to list folder names within a directory

    Thanks guys =]

  5. #5
    Registered User
    Join Date
    11-17-2005
    Posts
    6

    Re: Macro to list folder names within a directory

    Is there any way to modify this macro to capture not only folder names within the top folder, but also 2nd-level subfolders?

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to list folder names within a directory

    Hello Mustang,

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread.

Closed Thread

Thread Information

Users Browsing this Thread

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

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