0

我需要处理大量文件并通过一组目录和子目录搜索并从找到的目录中获取文件。这很容易使用一个简单的目录,但是当搜索一千个目录时,程序会变慢到爬行。我听说过使用 enumeratefiles 但我不知道如何解决这个问题。谢谢你。

我应该如何处理这个?

 dim Filename_to_lookfor  as string 
 dim  filePaths As String()

for   i = 0 to 1000 
   Filename_to_lookfor = array(i)

   filePaths = Directory.GetFiles(sTargetPath, sFilename_to_lookfor.ToUpper, 
   IO.SearchOption.AllDirectories)

   if filepath.length > 0 then 
    'copy file using Computer.FileSystem.CopyFile()
   end if

 next

谢谢你。

4

1 回答 1

1

另一种编码是使用 Linq:

Sub SearchFiles(pathAndfileNames_to_lookfor() As String, sTargetPath As String)

    ' Call just once Directory.GetFiles():
    Dim FilePaths() As String = Directory.GetFiles(sTargetPath, "*.*", IO.SearchOption.AllDirectories)

    Dim lookup1 = FilePaths.ToLookup(Function(x) x.ToLower)
    Dim lookup2 = pathAndfileNames_to_lookfor.ToLookup(Function(x) x.ToLower)
    Dim filesInBoth = lookup1.SelectMany(Function(x) x.Take(lookup2(x.Key).Count).ToArray)
    For Each file In filesInBoth
        'copy file using Computer.FileSystem.CopyFile()
        Console.WriteLine(file)
    Next
End Sub

调用过程如下:

    Dim file1 As String = "C:\...\file1..."
    Dim file2 As String = "C:\...\file2..."

    Dim pathAndfileNames_to_lookfor() As String = New String() {file1, file2}
    Dim sTargetPath = "..."
    SearchFiles(pathAndfileNames_to_lookfor, sTargetPath)
于 2022-01-29T16:00:27.123 回答