我正在尝试搜索扩展名为“ .xml”的文件。单个路径下的多个目录中的“ .pdf”。
但问题是这些目录由许多子目录组成,我必须读取由“ .xml”和&组成的子目录。单个路径下的“ .pdf”文件如果子目录中缺少任何一个文件,则代码应该能够获取该特定的主目录名。
请有人帮我解决这个问题,
任何帮助都将非常感激
答案 0 :(得分:3)
由于VB中Stacks的可用性,我已经将它们用于递归,因为它调试简单,并且递归程序失控的可能性更小。
Function SearchForFiles(ByVal RootFolder As String, ByVal FileFilter() As String) As List(Of String)
Dim ReturnedData As New List(Of String) 'List to hold the search results
Dim FolderStack As New Stack(Of String) 'Stack for searching the folders
FolderStack.Push(RootFolder) 'Start at the specified root folder
Do While FolderStack.Count > 0 'While there are things in the stack
Dim ThisFolder As String = FolderStack.Pop 'Grab the next folder to process
Try 'Use a try to catch any errors
For Each SubFolder In GetDirectories(ThisFolder) 'Loop through each sub folder in this folder
FolderStack.Push(SubFolder) 'Add to the stack for further processing
Next 'Process next sub folder
For Each FileExt In FileFilter 'For each File filter specified
ReturnedData.AddRange(GetFiles(ThisFolder, FileExt)) 'Search for and return the matched file names
Next 'Process next FileFilter
Catch ex As Exception 'For simplicity sake
End Try 'We'll ignore the errors
Loop 'Process next folder in the stack
Return ReturnedData 'Return the list of files that match
End Function
确保在源文件的顶部包含Imports System.Io.Directory
。通话很简单:
Dim Files = SearchForFiles("D:\Programming_VS\", {"*.xml", "*.pdf"})
返回包含搜索文件的字符串列表。
答案 1 :(得分:1)
这至少应该让你开始。它使用递归来搜索文件夹和子文件夹中的文件。
Public Sub rec(ByVal SourcePath As String)
Dim SourceDir As DirectoryInfo = New DirectoryInfo(SourcePath)
Dim pathIndex As Integer
pathIndex = SourcePath.LastIndexOf("\")
' the source directory must exist, otherwise throw an exception
If SourceDir.Exists Then
Dim SubDir As DirectoryInfo
For Each SubDir In SourceDir.GetDirectories()
Console.WriteLine(SubDir.Name)
rec(SubDir.FullName)
Next
For Each childFile As FileInfo In SourceDir.GetFiles("*", SearchOption.AllDirectories).Where(Function(file) file.Extension.ToLower = ".pdf" Or file.Extension.ToLower = ".docx")
Console.WriteLine(childFile.Name)
Next
Else
Throw New DirectoryNotFoundException("Source directory does not exist: " + SourceDir.FullName)
End If
End Sub
样本用法
rec("C:\MyFiles")
答案 2 :(得分:0)
您可以遍历文件夹中的所有文件和子文件夹。
Const startDir = "c:\"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(startDir)
Traverse(oFolder)
Sub Traverse(oFldr)
For Each oSubFolder In oFldr.SubFolders
TraverseoSubFolder
Next
For Each oFile In oFldr.Files
//search for the files with extension you required
Next
End Sub
答案 3 :(得分:0)
通过搜索目录中的所有文件夹和子文件夹来返回文件的文件路径。 作为参数传递给以下函数的位置
Dim filepath As String = ""
Dim SourceDir As DirectoryInfo = New DirectoryInfo(AppSettings.Get("mapped_location") + "\AA7BB3B5\TIFF\")
For Each childFile As FileInfo In SourceDir.GetFiles("*", SearchOption.AllDirectories).Where(Function(file) file.Extension.ToLower = ".tif")
If childFile.FullName.ToString.Contains(sSelectedfile) Then
filepath = childFile.FullName
End If
Next
Return filepath
End Function