Results 1 to 8 of 8

Thread: [SOLVED] GetFiles - Multiple File Ext Patterns

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    [SOLVED] GetFiles - Multiple File Ext Patterns

    Hey all,

    I am using the below code to fill a listbox with just exensions I want.

    If I just do "*.jpg" by itself it works fine. Loads all files with that extension

    However, I am trying to add multiple extensions patterns, but my list box goes blank.

    Code:
    Dim filesI As New List(Of String)
    Dim fileStrI
    fileStrI = txtIMG.Text & "\"
    filesI.AddRange(IO.Directory.GetFiles(fileStrI, "*.jpg,*.png").  '<--- DOESN'T WORK AT RUNTIME - NO ERRORS AT DESIGNTIME
        Select(Function(f) IO.Path.GetFileName(f)))
    ListBox3.Items.Clear()
    ListBox3.Items.AddRange(filesI.ToArray)
    I know I don't have this right.
    I have been doing a lot of digging on Google and many offer sample code, but none if them seem to work.

    Can any one help me with this??

    Thanks in advance
    Last edited by pixelink; Jul 10th, 2020 at 05:12 AM.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: GetFiles - Mutiple File Ext Patterns

    Well of course it does not work as the documentation states https://docs.microsoft.com/en-us/dot...System_String_

    Code:
    dir.getfiles.where(function(fi) fi.extension = ".png" orelse fi.extension = ".jpeg").toarray
    If you are using LINQ EnumerateFiles seems more fitting.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: GetFiles - Mutiple File Ext Patterns

    vb.net Code:
    1. Private Function GetFiles(folderPath As String, ParamArray patterns As String()) As String()
    2.     Dim filePaths As IEnumerable(Of String) = New String() {}
    3.  
    4.     For Each pattern In patterns
    5.         filePaths = filePaths.Concat(Directory.EnumerateFiles(folderPath, pattern))
    6.     Next
    7.  
    8.     Return filePaths.ToArray()
    9. End Function
    Sample usage:
    vb.net Code:
    1. Dim filePaths = GetFiles(folderPath, "*.jpg", "*.png")
    That said, your code could do with some improving. I would tend to use DirectoryInfo and FileInfo and bind to the ListBox. That way, you can display the file name but still access the full path via the SelectedValue.
    vb.net Code:
    1. Private Function GetFileInfos(folderPath As String, ParamArray patterns As String()) As FileInfo()
    2.     Dim folder As New DirectoryInfo(folderPath)
    3.     Dim files As IEnumerable(Of FileInfo) = New FileInfo() {}
    4.  
    5.     For Each pattern In patterns
    6.         files = files.Concat(folder.EnumerateFiles(pattern))
    7.     Next
    8.  
    9.     Return files.ToArray()
    10. End Function
    Sample usage:
    vb.net Code:
    1. With ListBox3
    2.     .DisplayMember = "Name"
    3.     .ValueMember = "FullName"
    4.     .DataSource = GetFileInfos(folderPath, "*.jpg", "*.png")
    5. End With
    You can replace all your code with that. When the user selects a file by name from the list, you can get the full path of the file from the SelectedValue of the ListBox.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: GetFiles - Mutiple File Ext Patterns

    @jmc

    The second suggestion works fine.

    Thanks for your help
    Last edited by pixelink; Jul 10th, 2020 at 05:05 AM.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: [SOLVED] GetFiles - Mutiple File Ext Patterns

    Then I can only conclude that you must have no JPG or PNG files in the folder you specified. I just tested that exact code and it worked just as expected.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: [SOLVED] GetFiles - Mutiple File Ext Patterns

    Oops... it loads the appropriate extensions fine.

    However, when i select a file name from list box it generate an error.
    "Conversion from type 'FileInfo' to type 'String' is not valid."


    Code for selecting image file to display in picurebox

    Code:
    If ListBox3.Items.Count = 0 Then Exit Sub
        pbIMG.Image = Nothing
       'Dim img = System.Drawing.Image.FromFile(txtIMG.Text & "\" & ListBox3.SelectedItem)
       Dim img = System.Drawing.Image.FromFile(ListBox3.SelectedItem)
       pbIMG.Image = img
    I need a string to load image, not fileinfo

    How do I convert that?
    Last edited by pixelink; Jul 10th, 2020 at 05:17 AM.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: [SOLVED] GetFiles - Mutiple File Ext Patterns

    It does load... I just forgot to remove a line in the block.

    But, now the image doesn't load in picture box... see post just above
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: [SOLVED] GetFiles - Mutiple File Ext Patterns

    Oh, I got it. Forgot to switch "ListBox3.SelectedItem" for "ListBox3.SelectedValue"

    final listbox code that works:

    Code:
    If ListBox3.Items.Count = 0 Then Exit Sub
       pbIMG.Image = Nothing
       Dim img = System.Drawing.Image.FromFile(ListBox3.SelectedValue)
    Thanks @JMC again!!
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width