Adding File Names In A Folder To a Listbox
I wanted to add all the names of the files in a folder in my computer to a list box in my vb program, so i used:
Code:
Do
List1.AddItem Dir("G:\foler1\folder2\")
Loop
So i expect to get the file names in the path i've chosen ("G:\foler1\folder2\") to be added to List1. But there are 2 problems:
- the computer doesn't stop looping, so i need a code to tell it stop if there are no more files
- if the loop works, still the the program only adds the first file name to the listbox and keeps adding the first one in the loop.
a little help on this please, asap. thank u :)
Re: Adding File Names In A Folder To a Listbox
Try this...
Code:
Sub addfilenames()
Dim fold As Scripting.Folder
Dim fil As File
Dim fso As New FileSystemObject
'Replace this with your folder name
Set fold = fso.GetFolder("S:\aaa")
For Each fil In fold.Files
List1.AddItem fil.Name
Next fil
End Sub
Edit: Set reference to Microsoft Scripting Runtime library
Re: Adding File Names In A Folder To a Listbox
I wouldn't wanna add a dependency to the scripting object for listing files...
Code:
Dim strPath As String, strFile As String
strPath = "G:\foler1\folder2\"
strFile = Dir$(strPath)
Do While Len(strFile) > 0
List1.AddItem strFile
strFile = Dir$()
Loop
I think sometimes it might return ".." as a "go back to previous directory" so you might also need to add:
Code:
Dim strPath As String, strFile As String
strPath = "G:\foler1\folder2\"
strFile = Dir$(strPath)
Do While Len(strFile) > 0
If strFile <> ".." Then List1.AddItem strFile
strFile = Dir$()
Loop
Edit: There's also the FileListBox control in the toolbar...you can just set its path to whatever folder you want to list files from.
Re: Adding File Names In A Folder To a Listbox
Quote:
I wouldn't wanna add a dependency to the scripting object for listing files...
just curious.... why is that?
The reason is that I don't see any harm in using scripting object for listing files....
Re: Adding File Names In A Folder To a Listbox
Quote:
Originally Posted by koolsid
Try this...
Code:
Sub addfilenames()
Dim fold As Scripting.Folder
Dim fil As File
Dim fso As New FileSystemObject
'Replace this with your folder name
Set fold = fso.GetFolder("S:\aaa")
For Each fil In fold.Files
List1.AddItem fil.Name
Next fil
End Sub
Edit: Set reference to Microsoft Scripting Runtime library
I tried that and it gives me error msgs saying:
Quote:
Compile error: User-defined type not defined
and the problem seams to be the "As Scripting.Folder", "As File",.... in the dim statements.
when i get rid of the "As File" and "As Scripting.Folder", "As New FileSystemObject" another error msg comes up saying "object required" for this part of the code:
Quote:
Set fold = fso.GetFolder("G:\folder2/folder1/")
But anyways i think the FileListBox works for me, do you know what the property in the FileListBox that contains the file name is called? (e.g File1.text?? or file1. ? )
Re: Adding File Names In A Folder To a Listbox
That's because as I have mentioned in my post at the bottom... you need to set reference..to Microsoft Scripting Runtime library
Go to the "Project" menu, and select "References". You will be presented with a long list of available references, just scroll down to Microsoft Scripting Runtime library and select it
also
Set fold = fso.GetFolder("G:\folder2/folder1/")
should be
Set fold = fso.GetFolder("G:\folder2\folder1")
I have tested it and it works...
Re: Adding File Names In A Folder To a Listbox
Quote:
Originally Posted by koolsid
just curious.... why is that?
The reason is that I don't see any harm in using scripting object for listing files....
If he was already using FSO for something else in his project then I would agree. But I just don't see the point in add 148KB to the project just to list files when there is a just as easy to way to do it using the built-in Dir$() function. Or the FileListBox. ;)
Re: Adding File Names In A Folder To a Listbox
I agree with DigiRev.
However, no need to test for "." and ".." as both of these are directories
when you use Dir$(sPath & "\"), that will return a normal file only (but not hidden file).
Code:
Dim sFName As String
'-- noted that the path must ending with "\"
sFName = Dir$("G:\foler1\folder2\")
While Len(sFName)
List1.AddItem sFName
sFName = Dir$()
Wend
If using FSO, there are two ways Early or Late binding:
1. Late Binding (no need to set reference)
Code:
Dim fso As Object
Dim fo As Object
Dim fi As Object
Set fso = CreateObject("Scripting.FileSystemObject")
'-- the path below may be ended with "\" or not
Set fo = fso.GetFolder("G:\foler1\folder2")
For Each fi In fo.Files
List1.AddItem fi.Name
Next
Set fi = Nothing
Set fo = Nothing
Set fso = Nothing
2. Early Binding : "Microsoft Scripting Runtime" needs to be referenced.
Code:
Dim fso As Scripting.FileSystemObject
Dim fo As Scripting.Folder
Dim fi As Scripting.File
Set fso = New Scripting.FileSystemObject
Set fo = fso.GetFolder("G:\foler1\folder2")
For Each fi In fo.Files
List1.AddItem fi.Name
Next
Set fi = Nothing
Set fo = Nothing
Set fso = Nothing
To matching line-by-line with Late binding, I didn't use
Code:
Dim fso As New Scripting.FileSystemObject
It seems to be using Dir$() method is much simpler and faster, it takes less than half the time of the other methods.