View Full Version : getting the names of the sub directories


smiler44
08-09-2008, 07:17 AM
I wish to get the names of the sub directories in the C:\temp directory and display them in a lsitbox or combo box.

I know the sub directories are red, green and yellow.

The best code I can find gives me in a list box
c:\temp
c:\temp\red
c:\temp\green
c:\temp\yellow

all I want in the list box or combo box is
red
green
yellow

thank you in advance
Smiller44

issue resolved

RuralGuy
08-09-2008, 06:35 PM
Perhaps you will find this link (http://allenbrowne.com/ser-59.html) useful.

smiler44
08-10-2008, 10:59 AM
thanks RuralGuy. I got a little lost with the code and could not follow it.
I have managed to resolve my issue by using some code I found on the web and modifing it.

Below is what I have come up with and seems to work well in it's own project.


Option Explicit
Private Sub CmdStart_Click()
Static running As Boolean
Dim AllDirs As New Collection
Dim next_dir As Integer
Dim dir_name As String
Dim sub_dir As String
Dim i As Integer
Dim path As String

path = "C:\temp" 'this is the directory to look in to find sub directories
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''
'this code is not really required
'If running Then
' running = False
' cmdstart.Enabled = False
' cmdstart.Caption = "Stopping"
' Else
' running = True
' MousePointer = vbHourglass
' cmdstart.Caption = "Stop"
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''
DoEvents

next_dir = 1
'AllDirs.Add starttext.Text ' Start here.
AllDirs.Add path
Do While next_dir <= AllDirs.Count
' Get the next directory to search.
dir_name = AllDirs(next_dir)
next_dir = next_dir + 1

' Read directories from dir_name.
sub_dir = Dir$(dir_name & "\*", vbDirectory)
Do While sub_dir <> ""
' Add the name to the list if
' it is a directory.
If UCase$(sub_dir) <> "PAGEFILE.SYS" And _
sub_dir <> "." And sub_dir <> ".." _
Then
sub_dir = dir_name & "\" & sub_dir
On Error Resume Next
If GetAttr(sub_dir) And vbDirectory _
Then AllDirs.Add sub_dir
End If
sub_dir = Dir$(, vbDirectory)
Loop
DoEvents
If Not running Then Exit Do
Loop

' Update the display.
For i = 1 To AllDirs.Count

Text1.Text = AllDirs(i)

Call forme ' this sub checks text box and adds result to combo box

Next i
'''''''''''''''''''''''''''''''''''''''''
'this code not really required
' MousePointer = vbDefault
' cmdstart.Caption = "Start"
' cmdstart.Enabled = True
' running = False
'''''''''''''''''''''''''''''''''''''''''''
End Sub

Private Sub forme()
' checks whats in text 2 and if a name of a subdirectory adds it to a combo box
Dim scan As String
Dim name As String 'name is the name to be displayed in text2
scan = Text1.Text
Text2 = Mid$(scan, 9, 20) ' scans text2 starting at 9th charector and 'captures the next 20
name = Text2.Text
If Text2 <> "" Then
Combo1.AddItem name
Else
Exit Sub
End If
End Sub

RuralGuy
08-10-2008, 12:55 PM
Excellent! The important thing is that you resolved your issue.