Looping through files and putting them in a control


This VB source snippet demonstrates many common programming constructs. It demonstrates the list box control, which is used in virtually every Windows application ever made. Further it demonstrates the do while loop construct. This allows you in programming languages to loop through every item in a set. Lastly, this code sample demonstrates how to interact some with the File system. Specifically we see how to loop through all the files in a given folder.

To use this code snippet create a new VB6 project. Drag a button onto the form. Double click the button. This will take you to the buttons click event handler method. Inside this method add the following code. Run the application, click the button, and check it out.

  1. Dim TPath As String
  2. Dim Filename As String
  4. TPath = "C:\Windows\"
  5. Filename = Dir(TPath)
  7. Do While Filename <> ""
  8.     Filename = Dir
  9.     List1.AddItem TPath & Filename
  10. Loop

Obviously this just barely scratches the surface as to what we can do with loops, controls, and files, but it gives us a simple example to start with. If you would like to go more in depth in any of these areas check out the links below.

Note: The source for this was found at DreamVB which is no longer online.

If you enjoyed this post, subscribe for updates (it's free)

basket jordan

This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post.!


A good informative post that you have shared and thankful your work for sharing the information. Got some appealing information and would like to provide it a try. Applaud your work and keep sharing your information.


Hey, very nice blog! Beautiful and Amazing. I will bookmark your blog and take the feeds also

just a little bit request

how about if Im going to show only a swf files on the specified folder, I prefer like /games