The below code will list out the sub directory and list of files under c: and it will load it in combo box(combo1), but if you want to drill sub-sub directory level then you will have to use some recursion logic to get it,
Private Sub Form_Load() Get_All_SubDirectories End Sub Sub Get_All_SubDirectories()
Dim arSubDir() As String Dim sSubDir As String
sSubDir = GetSubDir("c:\")
If LenB(sSubDir) <> 0 Then arSubDir = Split(sSubDir, ";") For i1 = 0 To UBound(arSubDir) Combo1.AddItem arSubDir(i1) Next i1 End If
End Sub
Function GetSubDir(ByVal sPath As String, Optional ByVal sPattern As Variant) As Variant
Dim sDir As String Dim sDirLocationForText As String
On Error GoTo Err_Clk
If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"
If IsMissing(sPattern) Then sDir = Dir$(sPath, vbDirectory) Else sDir = Dir$(sPath & sPattern, vbDirectory) End If ' ----------------------------------------------------------- ' Coded by Shasur for http://vbadud.blogspot.com ' -----------------------------------------------------------
Do Until LenB(sDir) = 0
' ----------------------------------------------------- ' This will be the location for the sub directory ' ----------------------------------------------------- If sDir <> "." And sDir <> ".." Then sDirLocationForText = sDirLocationForText & ";" & sPath & sDir End If sDir = Dir$
Loop
If Left$(sDirLocationForText, 1) = ";" Then sDirLocationForText = Right(sDirLocationForText, Len(sDirLocationForText) - 1) GetSubDir = sDirLocationForText
Err_Clk: If Err <> 0 Then Err.Clear Resume Next End If End Function
If you want a recursive one, then you can look into the below kb article,
How to recursively search directories by using Visual Basic 2005 or Visual Basic .NET
http://support.microsoft.com/kb/306666
hope it helps! |