Search code examples
c#robocopy

copying files to another location from text file


We're working on moving files from one server to another. The last time we did this, we used vb Script. We move around a million files (took several days).

We're looking to speed up the process, as this looks like it's going to be a recurring process with possibly more files to be moved going forward.

Parameters:

  • Files to move dictated by two text files with multiple lines
  • Multiple files will be associated with each line in .txt files
  • Will need to search for files with parameters from text files
  • Copy the found files over to the new drive

Here's the idea:

  • Store all parameters into an array
  • Loop through the array and find the met parameters using something like this:
    • System.IO.Directory.GetFiles("PATH").Where(s => s.Contains("three"));
  • If c# containerizes that search, then copy those files using RoboCopy so we can multithread

Question:

  • Can this be done?
  • If not is there a way to multithread the code in c# to move through this faster?

My experience with c# is what I've learned in the last few days. If you need some of the code I've created already, let me know.


Solution

  • I would suggest reversing the order to minimize the GetFiles:

    • Store all parameters into an array

    • Store all the files on the path:

      var files = Directory.GetFiles("PATH")
      
    • Loop through the array and find the met parameters:

      var ans = parameters.SelectMany(parameter => files.Where(s => s.Contains(parameter)))
                          .Distinct()
                          .ToList();
      
      • Then copy those files