Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List all folder names within a directory 1

Status
Not open for further replies.

rockfish12

Technical User
Feb 12, 2002
31
US
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
__________________________________________________

 
See if this works ?

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

'Turn back on if yoy want file names
'For Each filFile In fdrFolder.Files
' dctDict.Add filFile.Path, filFile.Path
' Debug.Print filFile
'Next filFile

Debug.Print strPath
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

 
Thanks, that worked like a charm!

I must confess, though, I don't know why. It doesn't make sense to me what's happening in that code.
 
Hi Rockfish

Its using a recursive function which means it calls on itself.

1) Send a Path into the GetFiles Function
2) the function loops through and prints all the file names
3) the function finds a sub folder and sends the path into
the GetFiles Function (Thus recursive) and starts at
step 1 for that folder.

What we did was comment out step 2 above and added a line to have it print out the name (path) of each folder it finds

1) Send a Path into the GetFiles Function
2) Prints Path sent in
3) the function finds a sub folder and sends the path into
the GetFiles Function (Thus recursive) and starts at
step 1 for that folder.


You can always place a break point somewhere in the code (Maybe the Set fdrFolder = fsoSysObj.GetFolder(strPath) line) and then use the F8 key to step through and watch how the code works. Recursive functions are nice but you can easily get into trouble with them.


Anyway hope it helps

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top