Your problem is that your loops don't work the way you think they do.
You inner loop always starts at one, so it always checks the same files list
What happens when i == 1? the first item you check it against is j == 1 which is itself. So naturally, it matches. So every file except the first will find at least one duplicate.
Why not use a better structure?
string[] files = Directory.GetFiles(@"C:/example", "*.txt", SearchOption.AllDirectories);
Dictionary<string, string> unique = new Dictionary<string, string>();
foreach (string path in files)
{
string file = Path.GetFileName(path);
if (!unique.ContainsKey(file))
{
unique.Add(file, path);
}
}
Updated code from OP:
for (int i = 0; i < arrfiles.Length; i++) {
for (int j = 0; j < arrfiles.Length -2; j++) {
fi = new FileInfo(arrfiles[i]);
fi2 = new FileInfo(arrfiles[j]);
Console.ReadLine();
if (Path.GetFileName(arrfiles[i]) == Path.GetFileName(arrfiles[j]) && i != j))
{ arrfilenames.Add(arrfiles[j]);
}
}
This first thing that spring into mind is that it doesn't find all duplicates:
Inputs:
D:\Temp\ContactsShort.txt
D:\Temp\ListSQL Server databases.txt
D:\Temp\myFile.txt
D:\Temp\myFileCopied.txt
D:\Temp\MyLargeTextFile.txt
D:\Temp\Tip - enum in combobox.txt
D:\Temp\New folder\ListSQL Server databases.txt
D:\Temp\New folder\myFile.txt
D:\Temp\New folder\myFileCopied.txt
D:\Temp\New folder\MyLargeTextFile.txt
Outputs:
D:\Temp\New folder\ListSQL Server databases.txt
D:\Temp\New folder\myFile.txt
D:\Temp\ListSQL Server databases.txt
D:\Temp\myFile.txt
D:\Temp\myFileCopied.txt
D:\Temp\MyLargeTextFile.txt
It has missed out the "New Folder" version of two of the files. There is also the problem that if you give it three files of the same name it reports four...
Secondly, it's not very efficient - you keep making the same comparisons over and over again. Why start with j at zero? you have already checked all the combinations before the current value of i anyway.
So, start the inner loop later, and throw away the test for i and j being the same. Then report both files when you find a match. (I have thrown out the FileInfo stuff, just for clarity)
for (int i = 0; i < files.Length; i++)
{
for (int j = i + 1; j < files.Length; j++)
{
if (Path.GetFileName(files[i]) == Path.GetFileName(files[j]))
{
arrfilenames.Add(files[i]);
arrfilenames.Add(files[j]);
}
}
}
You can also end the outer loop one iteration earlier without losing data if you want to.
As it is, this assumes a single duplication, and will always report pairs of files together (so it has the same second problem as yours. It also has some inefficiency built in. The solution is to remove entries when you have reported them:
for (int i = 0; i < files.Length - 1; i++)
{
bool reportNeeded = false;
string checkFile = Path.GetFileName(files[i]);
for (int j = i + 1; j < files.Length; j++)
{
if (files[j] != null && Path.GetFileName(files[j]) == checkFile)
{
reportNeeded = true;
arrfilenames.Add(files[j]);
files[j] = null;
}
}
if (reportNeeded)
{
arrfilenames.Add(files[i]);
}
}
(I renamed the folders to make it a bit more readable)
Inputs:
D:\Temp\ContactsShort.txt
D:\Temp\ListSQL Server databases.txt
D:\Temp\myFile.txt
D:\Temp\myFileCopied.txt
D:\Temp\MyLargeTextFile.txt
D:\Temp\Tip - enum in combobox.txt
D:\Temp\F 1\ListSQL Server databases.txt
D:\Temp\F 1\myFile.txt
D:\Temp\F 1\myFileCopied.txt
D:\Temp\F 1\MyLargeTextFile.txt
D:\Temp\F 2\ListSQL Server databases.txt
D:\Temp\F 2\myFile.txt
D:\Temp\F 2\myFileCopied.txt
D:\Temp\F 2\MyLargeTextFile.txt
Outputs:
D:\Temp\F 1\ListSQL Server databases.txt
D:\Temp\F 2\ListSQL Server databases.txt
D:\Temp\ListSQL Server databases.txt
D:\Temp\F 1\myFile.txt
D:\Temp\F 2\myFile.txt
D:\Temp\myFile.txt
D:\Temp\F 1\myFileCopied.txt
D:\Temp\F 2\myFileCopied.txt
D:\Temp\myFileCopied.txt
D:\Temp\F 1\MyLargeTextFile.txt
D:\Temp\F 2\MyLargeTextFile.txt
D:\Temp\MyLargeTextFile.txt