vb.net get filename list from wildcard

2019-03-05 05:06发布

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?

4条回答
smile是对你的礼貌
2楼-- · 2019-03-05 05:50

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

查看更多
干净又极端
3楼-- · 2019-03-05 05:52

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.

查看更多
该账号已被封号
4楼-- · 2019-03-05 06:03

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

查看更多
来,给爷笑一个
5楼-- · 2019-03-05 06:03

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
查看更多
登录 后发表回答