【问题标题】:How to search for files in directory and subdirectories using a single path如何使用单个路径搜索目录和子目录中的文件
【发布时间】:2014-04-23 15:53:15
【问题描述】:

我正在尝试在单个路径下的多个目录中搜索扩展名为“.xml”和“.pdf”的文件。

但这里的问题是目录由许多子目录组成,我必须读取由单个路径下的“.xml”和“.pdf”文件组成的子目录如果子目录中缺少任何一个文件,代码应该能够获取该特定的主目录名称。

请任何人帮助我解决这个问题,

任何帮助将不胜感激

【问题讨论】:

  • 不欢迎全部大写。

标签: vb.net visual-studio-2010 visual-studio


【解决方案1】:

自从在 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"})

返回包含搜索文件的字符串列表。

【讨论】:

    【解决方案2】:

    这至少应该让你开始。它使用递归来搜索文件夹和子文件夹中的文件。

    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")
    

    【讨论】:

    • 有没有办法在一个子目录中同时找到两个不同扩展名的文件..
    • 可以,使用childFile.extension获取你要查找的文件的扩展名,然后通过设置条件(if-else语句,select case等)获取逻辑
    • 看到我已经这样做了childFile As FileInfo In SourceDir.GetFiles("*.xml",searchoption.searchalldirectories) 在这个我能够找到 xml 文件并且无法插入另一个文件扩展名......所以有什么办法可以一起做......
    • 好的,现在我必须检查这两个文件是否在单个路径中所有目录的子目录中可用,如果缺少任何文件,那么它应该在文件丢失的 vch 中显示目录名称..不好意思打扰你了
    【解决方案3】:

    您可以遍历文件夹中的所有文件和子文件夹。

    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
    

    【讨论】:

      【解决方案4】:

      通过搜索文件夹中的所有文件夹和子文件夹返回文件的文件路径 作为参数传递给以下函数的位置

              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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-13
        • 1970-01-01
        • 2012-03-11
        • 2010-09-22
        • 2021-05-25
        • 2021-10-29
        • 1970-01-01
        • 2016-05-16
        相关资源
        最近更新 更多