Jump to content
New account registrations are disabed. This website is now an archive. Read more here.
Sign in to follow this  
formlesstree4

Lesson 6: Get all the files in a specified directory

Recommended Posts

This program will use several string manipulation functions. I suggest you ask questions if you don't understand what I'm doing, but I will break down what I do for each step.

(Video tutorial here on RapidShare since I've read people were having issues getting from Mediafire.)

 

Opening up Visual Studio, we're going to place one ListBox, one Button, and one FolderBrowserDialog. If you don't know where these are, look on the video tutorial so you have a reference.

Once placed, double click your button, and place the following code inside the Sub that Visual Studio was nice enough to create for us:

If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
   For Each f As String in IO.Directory.GetFiles(FolderBrowserDialog1.SelectedPath)
       Listbox1.Items.Add(f.Replace(String.Format("{0}\", SelectedFolderDialog.SelectedPath), String.Empty))
   Next
End If

 

Now, let's break this code down so you can understand it (for those that chose not to get the video).

1: If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

This line is very simple, it will display a Folder Browsing Dialog for you to select your folder to load the files from. If you find your file and press OK (or double click the directory in some cases), then the If Statement will be true, and the code inside the If statement will be executed.

 

2: For Each f As String in IO.Directory.GetFiles(FolderBrowserDialog1.SelectedPath)

This is the start of a loop. A loop will allow us to get all the files inside of the directory that we selected, hence the .SelectedPath at the end.

 

3: Listbox1.Items.Add(f.Replace(String.Format("{0}\", SelectedFolderDialog.SelectedPath), String.Empty))

This line is the biggest in the entire program. Basically, we were originally going to just add 'f', or the file including the path. I took it one step further invoking the .Replace() function of strings. Basically, we tell it what to replace and with what to replace it with. What the String.Format() returns is the entire FilePath, including the ending '\'. Then, after all of that, it adds the FileName to the ListBox in the order that they are found.

 

4: Next

This is just a call that will continue the loop until all values have been read from the IO.Directory.GetFiles() list.

 

 

Now, for your homework assignment. Using IO.Directory.GetFiles(), load all files into a listbox, and any text file, I want you to remove their extensions, as well as their leading paths. For example, let's say a file exists at: 'C:\foldername\text file.txt'. When you load it into the ListBox, it should be something like this: 'text file'. I shall have an example for you guys up later on tonight that will do just that.

Edited by formlesstree4
Adding video tutorial

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...