Hope you want Visual Basic version of code

sundar k replied to lokesh kumar at 10-May-08 06:09

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!




Click here to sign in and reply. You could earn money via our $500 contest just for being helpful.
  How to get a list of directory in c drive? - lokesh kumar  10-May-08 03:59 3:59:22 AM
      check here - Vasanthakumar D  10-May-08 04:26 4:26:57 AM
      Go thr this - Sujit Patil  10-May-08 04:52 4:52:18 AM
          re: Get directory - Chirag Bhavsar  10-May-08 05:21 5:21:14 AM
      Hope you want Visual Basic version of code - sundar k  10-May-08 06:09 6:09:18 AM
      Get C Drive directories - Sanjay Verma  12-May-08 08:09 8:09:29 AM
View Posts