Here's a simple VB6 code snippet that uses the FileSystemObjec t model to find all the files in C:\Temp and load their names into a listbox. To use this sample, you need to set things up as follows:
- Create a new project in VB6
- Add a form (this will probably happen by default, anyway)
- Pull down the Project menu and choose References.
- Find Microsoft Scripting Runtime and fill in the checkbox to select it. Click OK.
- Add a ListBox control to the form - name will be List1.
- Add a command button to the form - name will be Command1.
- Double-click on the command button to bring up the Click event procedure.
- Paste in the following code...
[CODE=vb]Dim fso As New FileSystemObjec t
Dim fld As Folder
Dim fil As File
Set fld = fso.GetFolder(" C:\Temp")
For Each fil In fld.Files
List1.AddItem fil.Name
Next
Set fil = Nothing
Set fld = Nothing
Set fso = Nothing[/CODE]Note, some browsers copy the line numbers, so you may need to edit them out before compiling the code. - Modify such details as the directory (C:\Temp) if required.
- Compile and run.
- Click the command button to fill the listbox with the names of the files in the specified directory.
Comment