vb.net get filename list from wildcard

人走茶凉 提交于 2019-12-02 19:45:51

问题


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\ *.txt an array of wildcard files?

a(0)=c:\debug\test1.txt
a(1)=c:\debug\test2.txt
a(2)=c:\debug\test3.txt

It is also possible that the string would be something like "C:\logs\12*\ *.log"

a(0)=C:\logs\120114\01.log
a(0)=C:\logs\120114\02.log
a(0)=C:\logs\120114\03.log

etc.

Anyone have any ideas on this?


回答1:


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




回答2:


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.




回答3:


See Directory.GetFiles (or EnumerateFiles), and also Directory.GetDirectories (or EnumerateDirectories)




回答4:


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


来源:https://stackoverflow.com/questions/24885637/vb-net-get-filename-list-from-wildcard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!