rockfish12
Technical User
Being very new to programming, I'm wondering how I can use VBA's File System Object to return a bunch of folder names from a specified directory. For example, if I specify the "c:\" directory, I would want to be able to output the names of all the subfolders within that directory to a text file.
I have borrowed the following code from MSDN, and used it successfully to return filenames. I cannot, however, get it to work for folder names. How can I modify this code to work for things other than just file names?
_________________________________________________
Function GetFiles(strPath As String, _
dctDict As Scripting.Dictionary, _
Optional blnRecursive As Boolean) As Boolean
Dim fsoSysObj As Scripting.FileSystemObject
Dim fdrFolder As Scripting.Folder
Dim fdrSubFolder As Scripting.Folder
Dim filFile As Scripting.File
Set fsoSysObj = New Scripting.FileSystemObject
On Error Resume Next
Set fdrFolder = fsoSysObj.GetFolder(strPath)
If Err <> 0 Then
GetFiles = False
GoTo GetFiles_End
End If
On Error GoTo 0
For Each filFile In fdrFolder.Files
dctDict.Add filFile.Path, filFile.Path
Debug.Print filFile
Next filFile
If blnRecursive Then
For Each fdrSubFolder In fdrFolder.SubFolders
GetFiles fdrSubFolder.Path, dctDict, True
Next fdrSubFolder
End If
GetFiles = True
GetFiles_End:
Exit Function
End Function
__________________________________________________
I have borrowed the following code from MSDN, and used it successfully to return filenames. I cannot, however, get it to work for folder names. How can I modify this code to work for things other than just file names?
_________________________________________________
Function GetFiles(strPath As String, _
dctDict As Scripting.Dictionary, _
Optional blnRecursive As Boolean) As Boolean
Dim fsoSysObj As Scripting.FileSystemObject
Dim fdrFolder As Scripting.Folder
Dim fdrSubFolder As Scripting.Folder
Dim filFile As Scripting.File
Set fsoSysObj = New Scripting.FileSystemObject
On Error Resume Next
Set fdrFolder = fsoSysObj.GetFolder(strPath)
If Err <> 0 Then
GetFiles = False
GoTo GetFiles_End
End If
On Error GoTo 0
For Each filFile In fdrFolder.Files
dctDict.Add filFile.Path, filFile.Path
Debug.Print filFile
Next filFile
If blnRecursive Then
For Each fdrSubFolder In fdrFolder.SubFolders
GetFiles fdrSubFolder.Path, dctDict, True
Next fdrSubFolder
End If
GetFiles = True
GetFiles_End:
Exit Function
End Function
__________________________________________________