zackmark15
zackmark15

Reputation: 727

Load multiple files by File-Type into ListView Control

How I can load files by File-Type?

I only wanna load the video files(.ts) from first the Loop and (.srt) files in the second Loop.

I tried, but I can't succeed in setting the Filter.

Here's the code by jmcilhinney

 Private Sub tbnAddFiles_Click(sender As Object, e As EventArgs) Handles tbnAddFiles.Click
    Dim vPaths As New List(Of String) 
    Dim vNames As New List(Of String)
    Dim sPaths As New List(Of String)
    Dim sNames As New List(Of String)

    Dim x As New OpenFileDialog
    x.Multiselect = True
    x.Filter = "TS and SRT File|*.ts;*.srt"
    x.RestoreDirectory = True

    If x.ShowDialog = DialogResult.OK Then

        'FOR VIDEO FILES (.ts)
        For Each f In x.FileNames
            vNames.Add(f)
            vPaths.Add(Path.GetFileName(f))
        Next

        'FOR SRT FILES (.srt)
        For Each f2 In x.FileNames
            sNames.Add(f2)
            sPaths.Add(Path.GetFileName(f2))
        Next

        AddToListView(vNames.ToArray, vPaths.ToArray, sNames.ToArray, sPaths.ToArray)

    End If
End Sub

Sub AddToListView(vNames As String(), sNames As String(), vPaths As String(), sPaths As String())
    LV.Items.Clear()

    Dim items As New List(Of ListViewItem)
    Dim upperBounds = {vNames.GetUpperBound(0), sNames.GetUpperBound(0), vPaths.GetUpperBound(0), sPaths.GetUpperBound(0)}

    For i = 0 To upperBounds.Min
        items.Add(New ListViewItem({vNames(i), sNames(i), vPaths(i), sPaths(i)}))
    Next

    LV.BeginUpdate()
    LV.Items.AddRange(items.ToArray())

    LV.EndUpdate()

End Sub

Upvotes: 0

Views: 161

Answers (2)

zackmark15
zackmark15

Reputation: 727

Here's the fixed and updated codes. Big thanks to @jmcilhinney

 Private Sub btnAddFiles_Click(sender As Object, e As EventArgs) Handles btnAddFiles.Click
    Using ofd As New OpenFileDialog
        With ofd
            .Filter = ("video and srt files (*.ts;*.srt)|*.ts;*.srt")
            .Multiselect = True
            .RestoreDirectory = True
        End With

        If ofd.ShowDialog = DialogResult.OK Then
            Dim vPaths As New List(Of String)
            Dim sPaths As New List(Of String)
            Dim vNames As New List(Of String)
            Dim sNames As New List(Of String)
            Dim oNames As New List(Of String)

            txtinputFolder.Text = Path.GetDirectoryName(ofd.FileName)

            For Each vid In ofd.FileNames.Where(Function(s) Path.GetExtension(s) = ".ts")
                vNames.Add(vid) 'input paths
                vPaths.Add(Path.GetFileName(vid)) 'intput filenames
                oNames.Add(Path.GetFileNameWithoutExtension(vid) & ".mkv") 'output file name
            Next

            For Each srt In ofd.FileNames.Where(Function(s) Path.GetExtension(s) = ".srt")
                sNames.Add(srt) 'input paths
                sPaths.Add(Path.GetFileName(srt)) 'input filenames
            Next

            AddToListView(vPaths.ToArray, sPaths.ToArray, oNames.ToArray, vNames.ToArray, sNames.ToArray)
        End If
    End Using
End Sub

Sub AddToListView(vPaths As String(), sPaths As String(), vNames As String(), sNames As String(), oNames As String())
    LV.Items.Clear()

    Dim items As New List(Of ListViewItem)
    Dim upperBounds = {vPaths.GetUpperBound(0), sPaths.GetUpperBound(0), vNames.GetUpperBound(0), sNames.GetUpperBound(0), oNames.GetUpperBound(0)}

    For i = 0 To upperBounds.Min
        items.Add(New ListViewItem({"Queued", vPaths(i), sPaths(i), vNames(i), sNames(i), oNames(i)}))
    Next

    LV.BeginUpdate()
    LV.Items.AddRange(items.ToArray())
    LV.EndUpdate()
    updateParameter()
End Sub

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54417

Filter the list using LINQ:

For Each f In x.FileNames.Where(Function(s) Path.GetExtension(s) = ".ts")
    vNames.Add(f)
    vPaths.Add(Path.GetFileName(f))
Next

'FOR SRT FILES (.srt)
For Each f In x.FileNames.Where(Function(s) Path.GetExtension(s) = ".srt")
    sNames.Add(f)
    sPaths.Add(Path.GetFileName(f))
Next

Upvotes: 1

Related Questions