Search for a file in a directory but that does not exist C #

0

I have to test that when searching for a file by name in a directory it does not exist and I indicate it on the screen. I show you my code, where is the comment of the interrogations is where I have the problem, the rest works correctly:

static void recorrerDirectoriosYCompararPorFicheros(string dir1, string dir2)
{
    System.IO.DirectoryInfo dirOrigenInfo = new DirectoryInfo(dir1);
    System.IO.DirectoryInfo dirDestinoInfo = new DirectoryInfo(dir2);

    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.FullName, file2.FullName);
            }
            else//????????????????????
            {
                Console.WriteLine("\nEl archivo {0} no existe en {1}", file.Name, dir2);

            }
        }

    }

}

Here I leave the result, it tells me comparison by comparison if it exists or not, what I want is to go through the directory and tell me only once if it exists or not, the file that does not exist is c.txt : Creation date or last modification are NOT the same The C: \ Projects \ OriginalFile \ a.txt and C: \ Projects \ Carpe files taDestino \ a.txt are not equal by creation date and last modification

  

The a.txt file does not exist in C: \ Projects \ TargetFile

     

The a.txt file does not exist in C: \ Projects \ TargetFile

     

The a.txt file does not exist in C: \ Projects \ TargetFile

     

The c.txt file does not exist in C: \ Projects \ TargetFile

     

The c.txt file does not exist in C: \ Projects \ TargetFile

     

Creation date and last modification are the same   C: \ Projects \ Source_folder \ c.txt and C: \ Projects \ FolderDestin or \ c.txt   They are the same by date of creation and last modification

     

The c.txt file does not exist in C: \ Projects \ TargetFile

     

The Configuration.ini file does not exist in C: \ Projects \ TargetFile

     

The Configuration.ini file does not exist in C: \ Projects \ TargetFile

     

The Configuration.ini file does not exist in C: \ Projects \ TargetFile

     

Creation date or last modification are NOT the same Files   C: \ Projects \ FolderOrigin \ Configuration.ini and C: \ Pr   ojects \ Destinationfolder \ Configuration.ini are not equal by date of   creation and last modification

    
asked by LopezAi 05.10.2017 в 11:04
source

2 answers

3

In your code you are taking each file from the source directory and comparing it with each of the existing files in the destination directory. In each comparison, if the name does not match, you conclude that the file does not exist (erroneously because it does not coincide with the first file does not mean that it will not coincide with another) and nevertheless you keep searching.

What you should do is look in all the destination files for the name of the source file and, in the end, conclude whether it was found or not.

To simplify the code you can use LINQ instead of nested loops:

static void recorrerDirectoriosYCompararPorFicheros(string dir1, string dir2)
{
    System.IO.DirectoryInfo dirOrigenInfo = new DirectoryInfo(dir1);
    System.IO.DirectoryInfo dirDestinoInfo = new DirectoryInfo(dir2);

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

    foreach (var file in fileDirOrigenNames)
    {
        var file2 = fileDirDestinoNames.FirstOrDefault(f => f.Name == file.Name);
        if (file2 == null)
        {
            Console.WriteLine($"\nEl archivo {file.Name} no existe en {dir2}");
        }
        else
        {
            mostrarResultadoComparacionPropiedades(file.FullName, file2.FullName);
        }
    }
}
    
answered by 05.10.2017 / 11:18
source
1

I would use Linq and Contains to verify that the name you are processing does not exist in the target collection:

for (int i = 0; i <= (fileDirOrigenNames.Length) - 1; i++)
{
    System.IO.FileInfo file = fileDirOrigenNames[i];
    if (!fileDirDestinoNames.Select(x=>x.Name).ToList().Contains(file.Name))
    {
        Console.WriteLine("\nEl archivo {0} no existe en {1}", file.Name, dir2);
    }
    else
    {
        for (int j = 0; j <= (fileDirDestinoNames.Length) - 1; j++)
        {
            System.IO.FileInfo file2 = fileDirDestinoNames[j];
            if (file.Name.Equals(file2.Name))
            {
                  mostrarResultadoComparacionPropiedades(file.FullName, file2.FullName);
            }
        }
    }
}
    
answered by 05.10.2017 в 11:16