Get Files Form Sub Folders
hi,
This is a small pice of code I did this mornning that allows you to give the function a folder and it will scan all folders and subfolders and store the files in a collection for latter use. anyway Hope this maybe of some use to someone.
Code:
Private Sub GetFiles(ByVal Path As String, FileAttr As VbFileAttribute, oFiles As Collection)
Dim x As String
Dim oFol As New Collection
Dim Item
On Error Resume Next
'Append back slash if needed
If Right(Path, 1) <> "\" Then Path = Path & "\"
'Current folder to scan
x = Dir(Path, FileAttr)
'Do until there no more files found
Do Until (x = "")
'Check if file is folder
If (GetAttr(Path & x) And vbDirectory) = vbDirectory Then
'Skip over . or ..
If Left(x, 1) <> "." Then
'Add the folder name to the collection for later use
Call oFol.Add(Path & x & "\")
End If
Else
'Add the filenames to the collection
Call oFiles.Add(Path & x)
End If
DoEvents
x = Dir()
Loop
'Loop tho the folders found
For Each Item In oFol
'Recall GetFiles
Call GetFiles(Item, FileAttr, oFiles)
Next Item
Set oFol = Nothing
End Sub
Example using the procedure above
Code:
Private Sub Command1_Click()
Dim Files As New Collection
Dim Item
'Get files and store them into the files collection
Call GetFiles("C:\Games", vbDirectory, Files)
For Each Item In Files
'Add the files from the collection into the listbox
Call List1.AddItem(Item)
Next Item
'Destroy collection
Set Files = Nothing
End Sub