Create a new project.
Add a button to your form (top left).
Add a label on the right of the button. Label text: No Folder Selected.
Below the button add a combobox.
Go to toolbox -> Dialogs -> doubleclick on the FolderBrowseDialog.
Double click on the button and go to the sub. Before we write anything inside we have to add this at the start of the program:
Imports System.IO
After "Public Class Form1" add this:
Private f1 As String()
We will load a folder and store the filenames with the folder path inside the "f1" variable.
Add this inside button1 sub:
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK ThenLabel1.Text = FolderBrowserDialog1.SelectedPath
f1 = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
End IfCode:
Imports System.IO
Public Class Form1
Private f1 As String()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
Label1.Text = FolderBrowserDialog1.SelectedPath
f1 = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
End If
End Sub
End Class
Public Class Form1
Private f1 As String()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
Label1.Text = FolderBrowserDialog1.SelectedPath
f1 = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
End If
End Sub
End Class
Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*") - this function will get us the filenames. We can specify one file extension filter in the arguments. If we wanted only txt files, we would do this:
Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.txt")
At this moment we can't modify the current arguments to add more extension filters inside the arguments of the "getfiles" function. We can't do something like this:
Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.txt, *.doc, *pdf") - or use | instead of commas.
Search "visual basic multiple file extension getfiles" in google for more info on this issue.
How to filter more file extensions?
We stored everything in "f1" so we can filter all the strings of "f1" for what we need.
Let's add another array variable that will contain the desired file extensions. Below "Private f1 as string" add:
Private g1 As String() = {".txt", ".pdf", ".docx"}
-> you can keep adding file extensions inside "g1" divided by a "," sign
We need to search each string of "f1". Inside of button1 sub, just before the "end if" line, we will - first - add:
For Each i In f1
next-> for each string inside "f1" -> do something. The "for" loop ends when it reaches the end of "f1".
Inside of the previous loop we insert another loop:
For i2 = 0 To g1.GetUpperBound(0)
NextThis "for" loop checks all the file extensions inserted inside "g1" at the beginning for each string of "f1".
-> we have used another variable for this loop: "i2" rather than using the previous one again: "i". That would cause problems. This "for" loop starts at 0 for each line of string from "f1".
- g1.getupperbound(0) - this will give us the highest number in the "g1" array. If the array has 3 values stored, then getupperbound will return the number 2, since the array starts from 0 and goes to 2. If the array has 10 values stored, getupperbound will return 9, which means 10 items from 0 to 9.
Inside the previous loop we will insert what to do with the strings (filenames) with the desired file extensions.
In the case of this program, we will add them to the combobox. Add this line inside:
If i.EndsWith(g1(i2)) Then
ComboBox1.Items.Add(i)
End If
If i.EndsWith(g1(i2)) Then - if a string from "f1" ends with any of the extensions present inside "g1", then add that string to the combobox.
Code:
Imports System.IO
Public Class Form1
Private f1 As String()
Private g1 As String() = {".txt", ".pdf", ".doc"}
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
Label1.Text = FolderBrowserDialog1.SelectedPath
f1 = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
For Each i In f1
For i2 = 0 To g1.GetUpperBound(0)
If i.EndsWith(g1(i2)) Then
ComboBox1.Items.Add(i)
End If
Next
Next
End If
End Sub
End Class
Public Class Form1
Private f1 As String()
Private g1 As String() = {".txt", ".pdf", ".doc"}
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
Label1.Text = FolderBrowserDialog1.SelectedPath
f1 = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
For Each i In f1
For i2 = 0 To g1.GetUpperBound(0)
If i.EndsWith(g1(i2)) Then
ComboBox1.Items.Add(i)
End If
Next
Next
End If
End Sub
End Class
Start program. Load a folder. Check the combobox if everything worked.