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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a filelist(inc Subdirectories) 1

Status
Not open for further replies.

ihaveaproblem

Programmer
Jul 4, 2003
14
GB
Does anybody know how to get a list of liles into an array using the DIR$ command. It needs to also read a varying number of subdirectoires which change on a ad hoc basis.

 
Add a standard module and paste the following code in it.
___

Option Explicit
Public Function GetFileList(ByVal Folder As String)
If Right$(Folder, 1) <> &quot;\&quot; Then Folder = Folder & &quot;\&quot;
Dim Files As New Collection
ScanFolder Folder, Files
Set GetFileList = Files
End Function
Private Function ScanFolder(Folder As String, Files As Collection)
Dim FolderItem As Variant, NewItem As String, Attr As Long
For Each FolderItem In FolderItems(Folder)
NewItem = Folder & FolderItem
On Error Resume Next
Attr = GetAttr(NewItem)
If Err Then GoTo NextItem
On Error GoTo 0
If Attr And vbDirectory Then
ScanFolder NewItem & &quot;\&quot;, Files
Else
Files.Add NewItem
End If
NextItem:
Next
End Function
Private Function FolderItems(Folder As String) As Collection
Dim C As New Collection, S As String
S = Dir(Folder, vbDirectory Or vbHidden Or vbSystem)
Do Until S = vbNullString
If S <> &quot;.&quot; And S <> &quot;..&quot; Then C.Add S
S = Dir
Loop
Set FolderItems = C
End Function
___

Place a list box (List1) on your form, and insert the following code in it.
___

Option Explicit
Private Sub Form_Load()
Dim Item As Variant
For Each Item In GetFileList(&quot;E:\Reference&quot;)
List1.AddItem Item
Next
End Sub
___

Run the program, it will list all the files in the folder E:\Reference including sub-folders in the listbox.
 
Thank you Hypetia , I found this can be used in MS ACCESS with Microsoft Forms ListBox.

________________________________________
Zameer Abdulla
Visit Me
There is only one perfect child in this world. Every mother has it.
 
Keep in mind that the listbox can only hold 32,768 items.

David
 
Let me confirm that I used Microsoft Forms ListBox which is an Activex Insert on Access forms. I was getting an error on using normal listbox of Access. I think normal listbox's column has a limit of 255 chars.

________________________________________
Zameer Abdulla
Visit Me
There is only one perfect child in this world. Every mother has it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top