Go through directory and compare its files with those of another directory C #

1

I have to take the files from a directory one by one and look in another directory for a file with the same name. Once they match, I have to call mostrarResultadoComparacionPropiedades() passing their paths as parameters. My code is this:

     static void recorrerDirectoriosYCompararPorFicheros(string dir1, string dir2)
     {
         System.IO.DriveInfo dirOrigen = new System.IO.DriveInfo(dir1);
         System.IO.DriveInfo dirDestino = new System.IO.DriveInfo(dir2);

         System.IO.DirectoryInfo dirOrigenInfo = dirOrigen.RootDirectory;
         System.IO.DirectoryInfo dirDestinoInfo = dirDestino.RootDirectory;

         System.IO.FileInfo[] fileDirOrigenNames = dirOrigenInfo.GetFiles("*.*");
         System.IO.FileInfo[] fileDirDestinoNames = dirDestinoInfo.GetFiles("*.*");

         for (int i = 0;i<=(fileDirOrigenNames.Length)-1;i++ )
         {
             System.IO.FileInfo file = fileDirOrigenNames[i];
             for (int j = 0; j <= (fileDirDestinoNames.Length)-1; j++)
             {
                 System.IO.FileInfo file2 = fileDirDestinoNames[j];
                 if (file.Name.Equals(file2.Name))
                 {
                     mostrarResultadoComparacionPropiedades(file.DirectoryName, file2.DirectoryName);
                 }
             }
         }
     }

Here are the methods to compare two files by properties:

    static bool compareFilesByProperties(string file1, string file2)
    {
        DateTime fechaCreacion1 = File.GetCreationTime(file1);
        DateTime ultimaModificacion1 = File.GetLastWriteTime(file1);

        DateTime fechaCreacion2 = File.GetCreationTime(file2);
        DateTime ultimaModificacion2 = File.GetLastWriteTime(file2);

        if ((fechaCreacion1.Date.Equals(fechaCreacion2.Date)) &&(ultimaModificacion1.Date.Equals(ultimaModificacion2.Date)))
        {
            Console.WriteLine("Fecha de Creación y última modificación son iguales");
            return true;
        }
        Console.WriteLine("Fecha de Creación o última modificación NO son iguales");
        return false;

    }

    static void mostrarResultadoComparacionPropiedades(string file1, string file2)
    {
        if (compareFilesByProperties(file1, file2)==true)
        {
            Console.WriteLine("Los  {0} y {1} son iguales por fecha de creación y por última modificación",file1, file2);
        }
        else
        {
            Console.WriteLine("Los archivos {0} y {1} no son iguales por fecha de creación y por última modificación", file1, file2);
        }
    }
    
asked by LopezAi 05.10.2017 в 09:30
source

2 answers

1

The problem you have is that you are not comparing the files that you believe:

System.IO.DriveInfo dirOrigen = new System.IO.DriveInfo(dir1);
System.IO.DriveInfo dirDestino = new System.IO.DriveInfo(dir2);

System.IO.DirectoryInfo dirOrigenInfo = dirOrigen.RootDirectory;
System.IO.DirectoryInfo dirDestinoInfo = dirDestino.RootDirectory;

You are accessing the root directory of the directories that you pass to the method ( dirOrigen.RootDirectory ). Remove the first two lines (you do not need DriveInfo for anything) and modify these lines to get the files of the directories that you pass to the method:

System.IO.DirectoryInfo dirOrigenInfo = new DirectoryInfo(dir1);
System.IO.DirectoryInfo dirDestinoInfo = new DirectoryInfo(dir2);
    
answered by 05.10.2017 / 10:24
source
1

According to what you show, you are comparing only the DateTime dates, without the hours, minutes, seconds or ticks, and I think your problem comes from there.

To be able to compare all that you must remove from your condition the .Date and compare the DateTime integer:

if ((fechaCreacion1.Equals(fechaCreacion2)) &&(ultimaModificacion1.Equals(ultimaModificacion2)))
    
answered by 05.10.2017 в 10:02