Get Folder Names

kirkm

Registered User.
Local time
Today, 23:02
Joined
Oct 30, 2008
Messages
1,257
I'm trying to get all (top?) Folder names in a specific path, but without any filenames or subfolders therein.

I presume I use the fso object somehow but can't quite figure it out or find any examples on Google.

Thanks for any help.
 
Not sure I'm clear on your question but..

Where is the path coming from?

would a split function work for your purposes?

Code:
Sub ParsePath()

    Dim strPath As String
    Dim VarItm As Variant
    Dim i As Integer
    
    strPath = "C:\First\Second\Third\Fourth\File.Pdf"

    VarItm = Split(strPath, "\")

    For i = 0 To UBound(VarItm) - 1 '(subtact 1 to exclude the file name, change "i = 0" to "i = 1" to exclude C:)
        Debug.Print VarItm(i)
    Next i

End Sub

'output
'C:
'First
'Second
'Third
'Fourth
 
I'm not sure want you want but if you want to list the subfolders in a folder then I think something like this does it:


Code:
Private Sub ListFolders(strFolder As String)
' use Microsoft Scripting Runtime for early binding
'Dim fso As FileSystemObject
'Dim RootFolder As Folder
'Dim fdr As Folder
Dim fso As Object
Dim RootFolder As Object
Dim fdr As Object
Dim strFolderNames As String
Set fso = CreateObject("Scripting.FileSystemObject")
Set RootFolder = fso.GetFolder(strFolder)
For Each fdr In RootFolder.SubFolders
    Debug.Print fdr.Name
Next fdr

End Sub

You can try this in the attached database.
 

Attachments

Me, riffing off other people's code...
Code:
Sub ParsePath()
[COLOR="Green"]'   moke's[/COLOR]
    Dim vItem
    
    For Each vItem In Split("C:\First\Second\Third\Fourth\File.Pdf", "\")
        Debug.Print vItem
    Next
End Sub

Private Sub ListFolders(strFolder As String)
[COLOR="Green"]'   steve's[/COLOR]
    Dim fdr As Object
    
    With CreateObject("Scripting.FileSystemObject")
         For Each fdr In .GetFolder(strFolder).SubFolders
             Debug.Print fdr.Name
         Next fdr
    End With
End Sub
 

Users who are viewing this thread

Back
Top Bottom