vb.net get filename list from wildcard

前端 未结 4 773
野趣味
野趣味 2021-01-27 13:48

I have string say \"c:\\debug\\ *.txt\" In Debug folder there are severeal .txt files , say test1.txt test2.txt test3.txt .

How can I get from this string c:\\debug\\ *.

相关标签:
4条回答
  • This should do it for you. It'll handle wildcards in directory part and filename part

    Private Function GetFiles(ByVal Path As String) As List(Of String)
    
        Dim drivePart As String, dirPart As String, filePart As String
    
        drivePart = Path.Substring(0, Path.IndexOf("\") + 1)
        dirPart = Path.Substring(Path.IndexOf("\") + 1, Path.LastIndexOf("\") - Path.IndexOf("\") - 1)
        filePart = Path.Substring(Path.LastIndexOf("\") + 1)
    
    
        Dim directories As New List(Of String)
        Dim files As New List(Of String)
    
    
        '' Walk directory tree finding matches
        '' This should handle wildcards in any part of the path
        Dim currentIndex As Integer = 0
        Dim directoryMatch As String() = dirPart.Split("\")
        For Each directory As String In directoryMatch
            WalkDirectories(drivePart, directories, directoryMatch, currentIndex)
            currentIndex += 1
        Next
    
        For Each directory As String In directories
            files.AddRange(System.IO.Directory.GetFiles(directory, filePart))
        Next
    
    
        Return files
    
    End Function
    
    Private Sub WalkDirectories(ByVal dirPart As String, ByVal directories As List(Of String), ByVal directoryMatch As String(), ByVal currentIndex As Integer)
    
        If currentIndex = directoryMatch.Length Then Return
    
        For Each d As String In System.IO.Directory.GetDirectories(dirPart, directoryMatch(currentIndex))
            directories.Add(d)
    
    
            WalkDirectories(System.IO.Path.Combine(dirPart, d), directories, directoryMatch, currentIndex + 1)
        Next
    End Sub
    

    Edit: just noticed that it wont handle UNC paths but it should be pretty easy to modify for that if you need to Editted again to handle multiple directory levels and wildcards at multiple levels (eg C:\debug\12*\log1*\errors*.txt

    0 讨论(0)
  • 2021-01-27 14:28

    I use the following code:

    Dim Path As String = "C:\debug"
    Dim Dir As New DirectoryInfo(Path)
    Dim q = (From x In Dir.GetFiles("*.txt", SearchOption.AllDirectories) Select x.FullName).ToArray
    

    You might need to

    Import System.IO
    Import System.Linq
    

    Basically your key for the requirement is SearchOption.AllDirectories which iterates through sub directories as well.

    0 讨论(0)
  • See Directory.GetFiles (or EnumerateFiles), and also Directory.GetDirectories (or EnumerateDirectories)

    0 讨论(0)
  • 2021-01-27 14:39

    Use the GetFiles from My.Computer.System and the ReadOnlyCollection(of String) from the system.collections.objectModel import and a searchoption as desired (top or all)

    sPath = "C:\debug"     ' your desired path
    sFile1 = "t*.txt"      ' your desired search pattern with * wildcard
    sFile2 = "test?.txt"   ' your desired search pattern with ? wildcard  
    
    dim lstFiles as system.collections.ObjectModel.ReadOnlyCollection(of String) = My.Computer.Filesystem.GetFiles(sPath, FileIO.SearchOption.SearchTopLevelOnly, sFile1)
    
    'lstfiles contains all the files that match your selection 
    'if you really need an array you can convert the list to array here
    
    dim i as integer = 0
    for each sFile as string in lstfiles
        a(i)=sfile
        i+=1
    next
    
    0 讨论(0)
提交回复
热议问题