GetFiles is incredibly slow vb.NET

313 views Asked by At

I need to process a large amount of files and scour thru a group of directories and subdirectories and get the file from the found directory. This is easy using a simple directory but when scouring a thousand directories, the program slows down to a crawl. I've heard of using enumeratefiles but I'm not how to approach this. thank you.

How should I approach this?

 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

Thank you.

1

There are 1 answers

0
Xavier Junqué On

Another coding would be using 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

Calling the procedure would be as follows:

    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)