+ Reply to Thread
Results 1 to 5 of 5

List of Folders

Hybrid View

  1. #1
    Registered User
    Join Date
    02-15-2006
    Posts
    93

    List of Folders

    Good morning all,

    I have an excel file that I am using to create an index. What I want to do is have excel list all of the folder names that are in the same directory as the excel file. I can get the full path name listed but not just the folder name. Any ideas?

    Thanks in advance,
    tc

  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,259
    Hello toocold,

    Here is a macro that will list the directory your are searching in cell "A1" of the Active Sheet, and all the folder names in it starting at cell "A2".
    Sub ListFolders(Optional FolderPath As String)
    
      Dim FolderName As String
      Dim R As Long
      
        R = 2
        If FolderPath = "" Then FolderPath = CurDir
        If Right(FolderPath, 1) <> "\" Then FolderPath = FolderPath & "\"
        
          ActiveSheet.Cells(1, "A") = FolderPath
          FolderName = Dir(FolderPath, vbDirectory)    ' Retrieve the first entry.
          
            Do While FolderName <> ""    ' Start the loop.
             'Ignore the current directory and the encompassing directory.
              If FolderName <> "." And FolderName <> ".." Then
               'Use bitwise comparison to make sure MyName is a directory.
                If (GetAttr(FolderPath & FolderName) And vbDirectory) = vbDirectory Then
                  ActiveSheet.Cells(R, "A") = FolderName
                  R = R + 1
                End If
              End If
              FolderName = Dir
            Loop
    
    End Sub
    Sincerely,
    Leith Ross

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

    Sub Test()
    Dim fs, f, r
    Set fs = CreateObject("Scripting.FileSystemObject")
    
    
    FolderName = Dir(ThisWorkbook.Path & "\", vbDirectory)
    Do While FolderName <> ""
        On Error Resume Next
        Set f = fs.GetFile(fs.GetFileName(FolderName))
        If FolderName <> f.Name Then
            N = N + 1
            Cells(N, 1) = FolderName
        End If
        FolderName = Dir()
    Loop
    End Sub
    Martin

  4. #4
    Registered User
    Join Date
    02-15-2006
    Posts
    93
    thanks guys, works great

  5. #5
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    This will use the currently selected file, and put the output in the file with the macro. That way you don't have to put the macro into the file you are testing....

    Sub aaa()
      Set fs = CreateObject("scripting.filesystemobject")
      Set outfile = ThisWorkbook.Sheets("sheet1")
      outfile.Range("A:A").ClearContents
      outfile.Range("A1").Value = ActiveWorkbook.Path
      For Each f In fs.getfolder(ActiveWorkbook.Path).subfolders
        outfile.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value = f
      Next f
      
    End Sub

    rylo

+ Reply to 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