Getting files with specific extensions using FolderBrowserDialog

Hi everyone.

I'm making a media player using the BASS API Library and Direct-X.

My problem is, I'm using the FolderBrowserDialog component and I know it can't filter files but I need only files with specific extensions to be added to the Listbox I have on the form.

So far the code looks like this:

Public Sub foldertoplaylist()

Dim plug As Integer = 2

Dim playlistid3 As String

Dim chosen As String

Dim filInfo As IO.FileInfo

AddFolder.Description = "Choose the folder you would like to add to Tray-Play"

If addfolder.ShowDialog = Windows.Forms.DialogResult.Cancel Then

Exit Sub

End If

chosen = AddFolder.SelectedPath

Directory.GetDirectories(chosen)

Directory.GetFiles(chosen)

For Each fil1 As String In IO.Directory.GetFiles(AddFolder.SelectedPath)

plug = Bass.BASS_StreamCreateFile(fil1, 0, 0, Un4seen.Bass.BASSStream.BASS_STREAM_DECODE)

filInfo = New IO.FileInfo(fil1)

Dim pluginfo As New Un4seen.Bass.AddOn.Tags.TAG_INFO

BassTags.BASS_TAG_GetFromFile(plug, pluginfo)

playlistid3 = pluginfo.artist & " - " & pluginfo.title

ID3Artist = pluginfo.artist

ID3Title = pluginfo.title

ID3Album = pluginfo.album

If playlistid3 = " - " Then

ListBox1.Items.Add(filInfo.Name)

ListBox2.Items.Add(filInfo.FullName)

Else

ListBox1.Items.Add(playlistid3)

ListBox2.Items.Add(filInfo.FullName)

End If

defaultplaylist()

Next

For Each dirgrab As String In IO.Directory.GetDirectories(AddFolder.SelectedPath)

For Each fil2 As String In IO.Directory.GetFiles(dirgrab)

plug = Bass.BASS_StreamCreateFile(fil2, 0, 0, Un4seen.Bass.BASSStream.BASS_STREAM_DECODE)

filInfo = New IO.FileInfo(fil2)

Dim pluginfo As New Un4seen.Bass.AddOn.Tags.TAG_INFO

BassTags.BASS_TAG_GetFromFile(plug, pluginfo)

playlistid3 = pluginfo.artist & " - " & pluginfo.title

ID3Artist = pluginfo.artist

ID3Title = pluginfo.title

ID3Album = pluginfo.album

If playlistid3 = " - " Then

ListBox1.Items.Add(filInfo.Name)

ListBox2.Items.Add(filInfo.FullName)

Else

ListBox1.Items.Add(playlistid3)

ListBox2.Items.Add(filInfo.FullName)

End If

defaultplaylist()

Next

Next

End Sub

That adds everything in the selected path folder and the sub folders (if any)

The problem is, its adds cover art (*.jpg, etc) to the list but all I want are the following extensions:

*.mod;*.mo3;*.s3m;*.xm;*.it;*.mtm;*.umx;*.mdz;*.s3z;*.itz;*.xmz;*.mp3;*.mp2;*.mp1;*.cda;*.wma;*.ogg;*.wav;*.aiff;*.ape;*.aac;*.m4a;*.flac;*.ams;*.avi;*.mpg;*.mpeg;*.wmv;*.mp4;*.asf;*.ogm;*.mkv;*.amp

Any ideas on how to filter for just the following formats from whats grabbed in the folders

thanks in advance.



Answer this question

Getting files with specific extensions using FolderBrowserDialog

  • brehmli

    Thank you ReneeC, I edited my code


  • HAV

    Yep, I wrote my first program with Visual Basic 6 version, then 7 and finally 8


  • DBatesX

    Your style still has a lot of VB6 influence. It was within the last year, almost exactly that I stopped coding in VB6. It's almost difficult to look at it now.

    The methods included in the string class, are really powerful (although I wish there were a few more). I do still use Mid at times because is will do somethings that methods string methods won't.

    But I byte my tongue when I do. ;)



  • Carlos Sbaraini Neto

    Here's a very short and sweet example:


    OPNFD.Filter = "Executable files (*.exe;*.cpl;*.bat)|*.exe;*.cpl;*.bat"

    OPNFD is the OpenFileDialogue... this is the syntax for the extension filter.



  • Aqazaa

    I already have 3 Open File Dialogs on the form, one for opening files, playlists, etc.

    Maybe I didn't explain myself very well. I'm trying to add a popular playlist function to my application, which is 'Add Folders to Playlist'.

    So far I'm using the code I have posted above to grab files from the selected path of the Folder Browser Dialog and then to navigate through any sub folders (if present) to grab the files inside them also.

    My only problem is that on adding folders it adds every file and I only need the specific set ones for audio/video, I don't want *.jpg, etc to be added to the list.

    If there any way to just get the files with the specified extensions from the way I'm grabbing the information so that it misses out adding *jpg's, etc

    Thanks in advance.

  • cnceric

    After For Each use this code :
        Dim dotIndex As Integer = fil1.LastIndexOf(".")
    Dim Extension As String = Mid(fil1, dotIndex + 2, fil1.Length - dotIndex)
    If Extension = "wav" Or Extension = "mp3" Then 'Put your extensions here...
    'Put your code here :)
    End If
    Hope this helps a bit...


  • Chris Karcher

    Private Function GetExtension(ByVal Fname As String) As String

    GetExtension = Nothing

    Dim dotptr As Integer = Fname.LastIndexOf(".")

    If dotptr = -1 Then Exit Function

    GetExtension = Fname.Substring(dotptr + 1).ToLower

    End Function



  • Rutger van Dijk

    You're welcome. May I guess that you have a VB6 background



  • Gert Drapers

    Thats exactly what I needed, thanks a lot !

    Took a wee bit of tweaking, but works brilliantly.

  • Getting files with specific extensions using FolderBrowserDialog