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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

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

    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.
  • winja
    New Member
    • Mar 2008
    • 1

    #2
    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.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      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 FileSystemObjec t to work pretty much the same.

      Please post any VB or VBScript questions in the Visual Basic forum.

      Comment

      • ganeshsraman21
        New Member
        • Mar 2019
        • 1

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

        Comment

        Working...