0

I have 2 directories ..I need to check that in two directories the files exist is same or not..if there is difference in files then move that files from first directory to second directoy..how can i implement?

Joseph
  • 11
  • 1
  • 4
    This is a bit too open ended question. Which part of this are you having trouble? – Sami Kuhmonen May 31 '22 at 07:22
  • i created 2 array for store file names from the directory.. – Joseph May 31 '22 at 07:25
  • "i created 2 array for store file names from the directory" ok please post this code. How do you compare the content of the suspicious file? – Mong Zhu May 31 '22 at 08:01
  • Are you sure you can't use [robocopy](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy) for this? `robocopy /xc /xn /xo /mov source destination ` – Matthew Watson May 31 '22 at 08:19

1 Answers1

0

Something like this?



        private void CopyMissedFiles(string source, string destination)
        {
            var sourceDir = new DirectoryInfo(source);
            var destinationDir = new DirectoryInfo(destination);
            var sourceFiles = sourceDir.GetFiles();
            var destinationFiles = destinationDir.GetFiles();
            foreach (var file in sourceFiles.Where(x => destinationFiles.All(y => y.Name != x.Name)))
            {
                file.CopyTo(Path.Combine(destinationDir.FullName, file.Name));
            }
        }

Update: if you need to compare the file content, you can use MD5 hash to do that. Here is the description, how to calculate the hash Calculate MD5 checksum for a file