That sounded easy, I thought that you could use the dir command to get the folders, but it doesen't work recursively. Anyway a combination of dir and the filesystemobject and a recurvise call does the trick:
------------------------------------------------------------
'Add a commandbutton and a list to a from and paste this code into the form.
Option Explicit
Dim LocalFileSystem
Private Sub Command1_Click()
Set LocalFileSystem = CreateObject("Scripting.FileSystemObject"
ScanDir "c:\"
End Sub
Private Sub ScanDir(TheDir As String)
Dim AFolder As Variant, TheFolders As Variant
Dim CurFile As String
'list all the htm files in this dir
CurFile = Dir(TheDir & "\*.htm"
While CurFile <> ""
List1.AddItem TheDir & "\" & CurFile
CurFile = Dir
Wend
Set TheFolders = LocalFileSystem.GetFolder(TheDir)
For Each AFolder In TheFolders.SubFolders
ScanDir (AFolder)
Next
End Sub
-----------------------------------------------------------
Once you make the decision to use the FileSystemObject, then it is pretty straightforward. Here's one method (note that it's pretty similar to sunaj's solution, but sticks with one file handling model, rather than two); you'll need to add a reference to the Microsoft Scripting Runtime. As with sunaj's code, add a listbox and command button to a form: [tt]
Option Explicit
Dim fso As New FileSystemObject
Dim myFolder As Folder
Dim myfile As File
Private Sub Command1_Click()
ScanFolderForFiles "C:\WorkShop" ' Or whatever...
End Sub
Private Sub ScanFolderForFiles(strFolder As String)
Dim tempFolder As Folder
Set myFolder = fso.GetFolder(strFolder) 'Root of current recursion ppoint
' List all the files in the current folder
For Each myfile In myFolder.Files
List1.AddItem (myfile.Path)
Next
' Examine all the subfolders of the current folder
For Each tempFolder In myFolder.SubFolders
ScanFolderForFiles tempFolder.Path
Next
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.