Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old August 23rd, 2007, 03:52 AM
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,750
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.



  #2  
Old March 6th, 2008, 04:34 AM
Newbie
 
Join Date: Mar 2008
Posts: 1

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


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.
  #3  
Old March 6th, 2008, 11:23 PM
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,750

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


Quote:
Originally Posted by winja
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.
Reply