Why the line im writing to a text file inside the loop for is written to the text file so many times?
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");
开发者_Go百科 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:
for (x = 0; x < images.Length - 1; x++) {
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 {
for (y = x + 1; y < images.Length; y++) {
...
The index for your inner loop is Y not X. So what you are currently doing is printing the element at index X, Y times within the inner loop - i.e. you are processing the element at X, Y times instead of once for the purposes of your output statements.
What you should do is move the block of code before the second loop. Checking that the file at X exists only once is enough, until X changes. So try:
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 {
for (y = x + 1; y < images.Length; y++)
{
// Etc
}
}
精彩评论