Why the line im writing to a text file inside the loop for is written to the text file so many times?
<pre><code>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 &lt; images.Length - 1; x++)
            {
                for (y = x + 1; y &lt; 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;
        }
</code></pre>

<p>This is the are problem:</p>

<pre><code>if (!File.Exists(images[x]))
                        {
                            Console.Write("The file " + images[x] + " is not exist\r");
                            sw.WriteLine("The file " + images[x] + " is not exist");
                        }
</code></pre>

<p>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 ?</p>

<p>Thanks.</p>
