using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication_FileComparison
{
class Program
{
static void Main(string[] args)
{
//renameFiles();
duplicateFilesFinder();
}
public static string duplicateFilesFinder()
{
Console.WindowWidth = 107;
byte[] a, b;
StreamWriter sw;
string sf;
string[] images;
int x = 0;
int y = 0;
sw = new StreamWriter(@"d:\stream.txt");
sf = @"d:\test";
images = Directory.GetFiles(sf, "*.jpg");
for (x = 0; x < images.Length - 1; x++)
{
for (y = x + 1; y < images.Length; y++)
{
Console.Write("Working on file " + images[x] + " please wait\r");
if (!File.Exists(images[x]))
{
Console.Write("The file " + images[x] + " is not exist\r");
sw.WriteLine("The file " + images[x] + " is not exist");
}
else
{
if (File.Exists(images[y]))
{
a = File.ReadAllBytes(images[x]);
b = File.ReadAllBytes(images[y]);
if (a.Length == b.Length)
{
Console.WriteLine("File " + images[x] + " is the same size as" + " " + images[y]);
sw.WriteLine("File " + images[x] + " is the same size as" + " " + images[y]);
File.Delete(images[y]);
Console.WriteLine("File " + images[y] + " have been deleted");
sw.WriteLine("File " + images[y] + " have been deleted");
}
}
}
}
}
sw.Close();
Console.WriteLine(Environment.NewLine + "Process finished please press any key to continue");
Console.ReadKey();
return sf;
}
This is the are problem:
if (!File.Exists(images[x]))
{
Console.Write("The file " + images[x] + " is not exist\r");
sw.WriteLine("The file " + images[x] + " is not exist");
}
If i dont put \r on the console.Write and using Console.WriteLine without \r i see in the consol window this images[x] file many times!
Same the second line the sw.WriteLine in the text file i see it many times there.
I want just to see it once if the file not exist show it once.
Why does it show it so mant times ? And how ot fix it ?
Thanks.
That’s because you are testing for the X file inside the Y loop. Put that test in the outer loop instead: