Looping through files and putting them in a control

Level:
Level1

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
  3.  
  4. TPath = "C:\Windows\"
  5. Filename = Dir(TPath)
  6.  
  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.!

Hi

Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often. ohio mesothelioma lawyer

www.abortion-facts.org

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.

seo

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

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.