Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get a list of folders 2

Status
Not open for further replies.

MJB3K

Programmer
Jul 16, 2004
524
0
0
GB
Hi, i was wondering, how do you get a list of folders from a dir (e.g. C:\) then display them in a list box??

any ideas?



Regards,

Martin

Gaming Help And Info:
 
You can try this...
Make sure that you've referenced Microsoft Scripting Runtime

Dim oFS As FileSystemObject
Dim oFolders As Folders
Dim oFolder As Folder
Dim itmX As ListItem
Dim sValue As String

Set oFolders = oFS.GetFolder(folderpath)
For Each oFolder In oFolders
sValue = oFolder.Name
Set itmX = lvListView.ListItems.Add
itmX.Text = sValue
itmX.Tag = Trim(sValue)
Next

Hope that helps!
 
To reference the Microsoft Scripting Runtime, in VB go to your menu item named Project, then select references. Good luck!
 
Dim itmX As ListItem - user defined type not defined

my code:

Code:
Private Sub Form_Load()
    
    Dim oFS As FileSystemObject
    Dim oFolders As Folders
    Dim oFolder As Folder
    Dim itmX As ListItem
    Dim sValue As String
    
    Set oFolders = oFS.GetFolder("C:\WINDOWS")
    For Each oFolder In oFolders
        sValue = oFolder.Name
        Set itmX = lvListView.ListItems.Add
                itmX.Text = sValue
                itmX.Tag = Trim(sValue)
    Next


        
End Sub

what do I call the listbox?

sorry for this!!


Regards,

Martin

Gaming Help And Info:
 
You can also do this without using FileSystemObject. There are plenty of examples on this forum. Here's another one.
___
[tt]
Private Sub Form_Load()
Dim Path As String, S As String
Path = "C:\Windows\" 'add the \ at the end
S = Dir$(Path & "*", vbDirectory)
While Len(S)
If S <> "." And S <> ".." Then
If GetAttr(Path & S) And vbDirectory Then
List1.AddItem S
End If
End If
S = Dir$
Wend
End Sub[/tt]
 
Or, of course, you could always use the DirListBox control included with VB...
 
I had forgotten you could get folders with Dir via the attributes... (duh)

here is a method to recurse through folders and return sub folders and their included files...

In my project, I use:
Form1 :: Form :: Main form
Text1 :: TextBox :: Output of folder structure data
...MultiLine = True
...ScrollBars = Both
Text2 :: TextBox :: Filter for extentions , or | seperated
Command1 :: CommandButton :: List Folders
Command2 :: CommandButton :: Save Text
Dir1 :: DirListBox :: Select Folder to List contents
CD1 :: CommonDialog :: Save As Dialog

Then insert this code in Form1...
Code:
Private Sub Form_Load()
  Text1.Text = ""
  Text2.Text = ""
  Command1.Caption = "List Folders"
  Command1.Default = True
  Command2.Caption = "Save Text"
  cd1.Filter = "Text|*.txt|All|*.*"
  cd1.DefaultExt = "txt"
  cd1.CancelError = True
End Sub

Private Sub Command1_Click()
  Text1 = ""
  [COLOR=blue][b]recurse_list Replace(Dir1 & "\", "\\", "\"), Text2[/b][/color]
End Sub

Private Sub Command2_Click()
  Err.Clear
  On Error Resume Next
  cd1.ShowSave
  If Err.Number = 0 Then
    If Dir(cd1.FileName) <> "" Then
      If MsgBox("File Exists" & vbCrLf & "Overwrite", vbYesNo, "Overwrite") = vbYes Then
        Open cd1.FileName For Output As #1
          Print #1, Text1
        Close
      End If
    Else
      Open cd1.FileName For Output As #1
        Print #1, Text1
      Close
    End If
  End If
End Sub

Function [COLOR=blue][b]recurse_list(Path As String, Optional ext As String, Optional tabs As String)[/b][/color]
  On Error Resume Next
  Dim Folders() As String
  Dim tmpExt() As String
  Dim t1 As String, t2 As String
  Folders = [COLOR=red][b]getFolders(Path)[/b][/color]
  For i = 0 To UBound(Folders)
    Text1 = Text1 & tabs & ">" & Folders(i) & vbCrLf
    [COLOR=blue][b]recurse_list Path & Folders(i) & "\", ext, tabs & vbTab[/b]][/color]
  Next
  File = Dir(Path & "*")
  While File <> ""
    If ext <> "" Then
      tmpExt = Split(Replace(ext, ",", "|"), "|")
      For i = 0 To UBound(tmpExt)
        t1 = UCase(Right(File, Len(tmpExt(i))))
        t2 = UCase(tmpExt(i))
        If t1 = t2 Then Text1 = Text1 & tabs & File & vbCrLf
      Next
    Else
      Text1 = Text1 & tabs & File & vbCrLf
    End If
    File = Dir()
  Wend
End Function

Function [COLOR=red][b]getFolders(Path As String) As String()[/b][/color]
  On Error Resume Next
  Dim Folder As String
  Dim Temp As String
  Folder = Dir(Path & "*", vbDirectory)
  While Folder <> ""
    If Folder <> "." And Folder <> ".." Then
      If GetAttr(Path & Folder) And vbDirectory Then
        Temp = Temp & IIf(Temp <> "", "|", "") & Folder
      End If
    End If
    Folder = Dir()
  Wend
  [COLOR=red][b]getFolders[/b][/color] = Split(Temp, "|")
End Function

With a few minor adjustments, you could send the data to an xml file instead...

See getFolders to get a list of sub folders... returns array of strings.

Enjoy ;-)
-Josh

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top