Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

Populate a list box with file names for a given folder... (VB 6)

Dim PathStr As String
Dim FileName As String

PathStr = "C:\MyFolder"

lstBox.Clear
.
.<code to cycle through file names in PathStr  folder to determine what and store file names in variable "FileName" >
.
lstBox.AddItem = FileName

Thanks
Avatar of Shauli
Shauli

Private Sub Form_Load()
Dim myFile As String
myFile = Dir("C:\MyFolder", vbDirectory)
    Do While Not myFile = vbNullString
        lstBox.AddItem myFile
        myFile = Dir
    Loop
End Sub

S
btw, maybe you want the FileListBox instead of the listbox. You'll find it in the basic components groups. To load the files into the filelistbox, all you have to do is:

Private Sub Form_Load()
     File1.Path = "C:\MyFolder"
End Sub

S
Avatar of Mike Eghtebas

ASKER

Short and sweet.

Could we limit them to a certain file extention?

Private Sub cboFolder_Click()                 ' Q1: or maybe cboFolder_Change()?
Dim myFile As String
myFile = Dir("C:\MyFolder", vbDirectory)
    Do While Not myFile = vbNullString
        if right(myFile,4)=".xls" Then lstBox.AddItem myFile     '<---  changed here
        myFile = Dir
    Loop
End Sub

Re:> filelistbox...

I will look at it.  SirBounty was talking about it too.
ASKER CERTIFIED SOLUTION
Avatar of Shauli
Shauli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sequence of operation:
- Select a folder from cboFolder (I like to keep them in a custom cboBox because they are few and change only from time to time)
- Select a folder from cboFolder.
- Your code above is OnClick event of cboFolder and works fine.

Btw, there is an item in cboFolder called <Browse..>, when user wants to add a new folder to the list, selects <Browse..> to fire your previouse code to bringup "looking for folder" interface.

Setup for last question:  When I use <Browse..> to add a new folder to the list (in OnClick event where I have following code as well:
Dim myFile As String
myFile = Dir("c:\MyFolder\*.xls", vbDirectory)
    Do While Not myFile = vbNullString
        List1.AddItem myFile
        myFile = Dir
    Loop
End Sub

And, because the folder name is not a part of cboFolder, above code doesn't work.  If there was event like AfterUpdate of Access, it would have been fine.  

Q1:  What other event I can move the code to correct the situatiuon?

Regards,

Mike
On minor problem I have is:
The above code doesn't work (if I get you properly) because you are looking for a folder, while the code reads: myFile = Dir("c:\MyFolder\*.xls", vbDirectory), which means that you are looking for xls files. I think yoy will have to replace the xls with *, so yoy can see all sub folders of C:\MyFolder. Otherwise you may want to take a look at the DirListBox component (again, part of the basic components). The DirListBox can work together with the FileListBox to achieve what yoy are after.

S
For some reason all "you" turned out as "yoy" :)

S