我有字符串说“c:\ debug \ * .txt” 在Debug文件夹中有严重的.txt文件,比如test1.txt test2.txt test3.txt。
如何从这个字符串中获取c:\ debug \ * .txt一个通配符文件数组?
a(0)=c:\debug\test1.txt
a(1)=c:\debug\test2.txt
a(2)=c:\debug\test3.txt
字符串也可能是“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
等
有人对此有任何想法吗?
答案 0 :(得分:1)
我使用以下代码:
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
您可能需要
Import System.IO
Import System.Linq
基本上,您需要的密钥是 SearchOption.AllDirectories ,它也会遍历子目录。
答案 1 :(得分:0)
请参阅Directory.GetFiles(或EnumerateFiles),以及Directory.GetDirectories(或EnumerateDirectories)
答案 2 :(得分:0)
这应该为你做。它将在目录部分和文件名部分
中处理通配符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
编辑:只是注意到它不会处理UNC路径,但如果需要,应该很容易修改它 再次编辑以处理多个级别的多个目录级别和通配符(例如C:\ debug \ 12 * \ log1 * \ errors * .txt
答案 3 :(得分:0)
使用My.Computer.System中的GetFiles和system.collections.objectModel导入中的ReadOnlyCollection(字符串)(根据需要)(以及顶部或全部)
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
答案 4 :(得分:0)
您可以使用“喜欢”关键字:
' For your example, call this function with root = "C:\logs" and wild = "12*\*.log"
' root is passed ByVal because it may get changed inside the function
Friend Function GetMyFiles(ByVal root As String, wild As String, Optional allowgap As Boolean = True) As List(Of String)
Dim a As New List(Of String), pattern As String
' ensure root ends with a \
If Not root.EndsWith("\") Then root &= "\"
' the extra * allows for subdirectories in between, if required
pattern = root & If(allowgap, "*", "") & wild
For Each f As String In My.Computer.FileSystem.GetFiles(root, FileIO.SearchOption.SearchAllSubDirectories)
If f Like pattern Then a.Add(f)
Next
Return a
End Function
当然,如果您访问受保护的系统目录,它就会失败。
这个功能只是为了演示'Like'关键字。
如果“root”不是驱动器根目录(例如 C:),它将起作用。
如果做得好,一个单独的函数将首先收集目录,每个目录都在 Try/Catch 块中测试访问权限。我感觉很慷慨,所以这是我获取所有可访问目录的代码:
Friend Function GetAllAccessibleDirs(ByRef Dir As String, Optional inclDir As Boolean = True, Optional Sort As Boolean = False) As List(Of String)
Dim D As New List(Of String), Q As New Queue(Of String), dummy As DirectoryInfo, s As String
If inclDir Then D.Add(Dir)
Q.Enqueue(Dir)
While Q.Count
For Each s In GetTopLevelDirs(Q.Dequeue)
Try
dummy = My.Computer.FileSystem.GetDirectoryInfo(s)
D.Add(s)
Q.Enqueue(s)
Catch
' Inaccessible folder
End Try
Next
End While
If Sort AndAlso D.Count Then D.Sort()
Return D
End Function
Friend Function GetTopLevelDirs(ByRef dir As String) As List(Of String)
Try
Return My.Computer.FileSystem.GetDirectories(dir, FileIO.SearchOption.SearchTopLevelOnly).ToList
Catch
Return New List(Of String)
End Try
End Function