Question How do I select a particular file extension through radio buttons by DirectoryInfo?

JohnSmith

Member
Joined
Mar 7, 2011
Messages
7
Programming Experience
Beginner
Hi All, I am new to programming. I wrote this simple code but it seemed not working at run time. I mean not selecting particular file extensions when I am selecting the particular radio buttons. By default each radio button is set to false on Checked property. Also there is no syntax error. Please guide me where I am wrong. I appreciate all in advance who respond to me query. Thank you all!

Code:
Imports System
Imports System.IO

Public Class ExDirec

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim drives() As String
        drives = Directory.GetLogicalDrives

        For Each drv In drives
            lstDrives.Items.Add(drv)
        Next

    End Sub

    Private Sub lstDrives_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDrives.SelectedIndexChanged

        Dim di As New DirectoryInfo(lstDrives.Text)
        Dim directories() As DirectoryInfo
        lstDirectories.Items.Clear()

        directories = di.GetDirectories()
        For Each dr In directories
            lstDirectories.Items.Add(dr)
        Next

    End Sub

    Private Sub lstDirectories_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDirectories.SelectedIndexChanged

        Dim di As New DirectoryInfo(lstDrives.Text & lstDirectories.Text)
        Dim fls() As FileInfo
        fls=di.GetFiles 
        lstFiles.Items.Clear()
        For Each fl In fls
            If rdbAll.Checked = True Then
                fls = di.GetFiles("*.*")
                lstFiles.Items.Add(fl.Name)
            ElseIf rdbText.Checked = True Then
                fls = di.GetFiles("*.txt")
                lstFiles.Items.Add(fl.Name)
            ElseIf rdbCab.Checked = True Then
                fls = di.GetFiles("*.cab")
                lstFiles.Items.Add(fl.Name)
            End If
        Next

    End Sub
End Class
 
Last edited by a moderator:

Menthos

Well-known member
Joined
Jul 18, 2006
Messages
276
Programming Experience
10+
Hi,

Not tried your code, but isn't your lstDirectories_SelectedIndexChanged() method pulling all the files from the chosen drive + folder and looping through that list. You are checking the radiobutton choice and running a second getfiles with a filter, but then are adding the 'Name' from your the current element in your loop.

Shouldn't you be checking the radiobutton outside of the loop and populating your 'fls' object at that point, then cycling through that object?
 

JohnSmith

Member
Joined
Mar 7, 2011
Messages
7
Programming Experience
Beginner
Well I modified the last part as you said Menthos. But it did not work. No change in the behavior. No only modification needed is in the lstDirectories_SelectedIndexChanged() method. Rest of the methods are perfectly fine I mean in there function. As you get the idea I have used list boxes and these three list boxes are named as lstDrives, lstDirectories and lstFiles. With three radio buttons, "All files","*.txt" and "*.cab" the only thing is that if I am checking the radio buttons outside of the loop the behavior is same. As you can see the code below. I am still trying different approaches to it. Let's see if more developers reply to this thread. I appreciate your reply Menthos. Thanks man!

Imports System
Imports System.IO

Public Class ExDirec


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim drives() As String

drives = Directory.GetLogicalDrives
For Each drv In drives
lstDrives.Items.Add(drv)
Next

End Sub

Private Sub lstDrives_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDrives.SelectedIndexChanged
Dim di As New DirectoryInfo(lstDrives.Text)
Dim directories() As DirectoryInfo
lstDirectories.Items.Clear()

directories = di.GetDirectories()
For Each dr In directories
lstDirectories.Items.Add(dr)
Next
End Sub
Private Sub lstDirectories_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDirectories.SelectedIndexChanged
Dim di As New DirectoryInfo(lstDrives.Text & lstDirectories.Text)
Dim fls() As FileInfo
fls = di.GetFiles
lstFiles.Items.Clear()

If rdbAll.Checked = True Then
For Each fl In fls
fls = di.GetFiles("*.*")
lstFiles.Items.Add(fl.Name)
Next
ElseIf rdbText.Checked = True Then
For Each fl In fls
fls = di.GetFiles("*.txt")
lstFiles.Items.Add(fl.Name)
Next
ElseIf rdbCab.Checked = True Then
For Each fl In fls
fls = di.GetFiles("*.cab")
lstFiles.Items.Add(fl.Name)
Next
End If

End Sub
End Class
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,412
Location
Norway
Programming Experience
10+
Use the code box when you post the code in the forums.

As Menthos said use your If conditions to get the correct file list first. Then afterwards do the loop on that list, eg:

Code:
declare files variable

If something
    files = this
else
   files = that
end if

for each file in files
    'do something with file
next
Instead of looping and adding items you could assign the file list to the ListBox DataSource property and set the DisplayMember property to "Name".
 

JohnSmith

Member
Joined
Mar 7, 2011
Messages
7
Programming Experience
Beginner
Thanks a million guys for your constant support. Yes Menthos was right. It worked. Thank you JohnH for the additional help.

SOLVED! Also JohnH please let me know how do I close this thread as I am new. Should I need to close it as the problem has been resolved. I mean this thread. I am sorry because I am asking such questions which I am unaware of.

Code:
Imports System
Imports System.IO

Public Class ExDirec


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim drives() As String

        drives = Directory.GetLogicalDrives
        For Each drv In drives
            lstDrives.Items.Add(drv)
        Next

    End Sub

    Private Sub lstDrives_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDrives.SelectedIndexChanged
        Dim di As New DirectoryInfo(lstDrives.Text)
        Dim directories() As DirectoryInfo
        lstDirectories.Items.Clear()

        directories = di.GetDirectories()
        For Each dr In directories
            lstDirectories.Items.Add(dr)
        Next
    End Sub
    Private Sub lstDirectories_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDirectories.SelectedIndexChanged
        Dim di As New DirectoryInfo(lstDrives.Text & lstDirectories.Text)
        Dim fls() As FileInfo
        fls = di.GetFiles
        lstFiles.Items.Clear()
        If rdbAll.Checked = True Then
            fls = di.GetFiles("*.*")
            For Each fl In fls
                lstFiles.Items.Add(fl.Name)
            Next
        ElseIf rdbText.Checked = True Then
            fls = di.GetFiles("*.txt")
            For Each fl In fls
                lstFiles.Items.Add(fl.Name)
            Next
        ElseIf rdbCab.Checked = True Then
            fls = di.GetFiles("*.cab")
            For Each fl In fls
                lstFiles.Items.Add(fl.Name)
            Next
        End If
    End Sub
End Class
 
Top Bottom