Results 1 to 5 of 5

Thread: [RESOLVED] Querying a list of FileInfo for a filename

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Resolved [RESOLVED] Querying a list of FileInfo for a filename

    I am using the code below to get a list of files from multiple folder paths. Now I want to query that list to see if a specified file name exists. I don't want to loop through the list one file at a time, but rather use something like LINQ, but I'm not sure how to do it. I'm hoping someone can help me out. Thanks...


    Code:
            Dim allFiles As New List(Of IO.FileInfo)
            Dim files As IO.FileInfo()
    
            files = New IO.DirectoryInfo(folderPath1).GetFiles("*")
            allFiles.AddRange(files)
    
            files = New IO.DirectoryInfo(folderPath2).GetFiles("*")
            allFiles.AddRange(files)
    
            files = New IO.DirectoryInfo(folderPath3).GetFiles("*", IO.SearchOption.AllDirectories)
            allFiles.AddRange(files)
    Last edited by nbrege; May 1st, 2023 at 12:23 PM.

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,525

    Re: Querying a list of FileInfo for a filename

    Can we assume that it does not matter what path the file name is in, or if it is in more than one path?

    Possibly useful links:

    https://learn.microsoft.com/en-us/do...s?view=net-8.0

    https://www.codeproject.com/Question...ist-using-linq

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Querying a list of FileInfo for a filename

    Quote Originally Posted by jdc2000 View Post
    Can we assume that it does not matter what path the file name is in, or if it is in more than one path?
    That is correct, path doesn't matter, and there should only be 1 copy of a file.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Querying a list of FileInfo for a filename

    You want LINQ's FirstOrDefault for this:-
    Code:
            'Name of file to find
            Dim findFile As String = "target.txt"
    
            'The first match found would be assigned to this variable. Nothing is assigned
            'if no matches were found.
            Dim foundFile As FileInfo = allfiles.FirstOrDefault(Function(f) f.Name.ToLower = findFile)
    
            'If a match was not found then 'foundFile' would be Nothing...
            If foundFile IsNot Nothing Then
                Debug.WriteLine($"{foundFile.FullName}, Size = {foundFile.Length.ToString}")
            End If
    
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Querying a list of FileInfo for a filename

    Niya, thank you, that worked perfectly...

Tags for this Thread

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