By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,011 Members | 2,968 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

FileSystemObject model - how to list files in a directory (VB6)

Expert 5K+
P: 8,434
Here's a simple VB6 code snippet that uses the FileSystemObject 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...
    Expand|Select|Wrap|Line Numbers
    1. Dim fso As New FileSystemObject
    2. Dim fld As Folder
    3. Dim fil As File
    4. Set fld = fso.GetFolder("C:\Temp")
    5. For Each fil In fld.Files
    6.   List1.AddItem fil.Name
    7. Next
    8. Set fil = Nothing
    9. Set fld = Nothing
    10. Set fso = Nothing
    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.
Aug 23 '07 #1
Share this Article
Share on Google+

P: 1
Hi, i am learning vbscript. Can you help me with vbscript, or is it only vb? I am a vitial impared person and find it difficult to get a good book on vbscript. I have some more questions, but enough for now. Thank you.
Mar 6 '08 #2

Expert 5K+
P: 8,434
Hi, i am learning vbscript ...
We're glad to try and help with VBScript, but we might not always be aware of the exact differences between that and VB. Certainly I'd expect FileSystemObject to work pretty much the same.

Please post any VB or VBScript questions in the Visual Basic forum.
Mar 6 '08 #3

P: 1
hi, iam learning vb6 now. i want to add all files in a folder to a list by using loop. i dont want to use for each loop. is it possible to do so...please help me
Mar 4 '19 #4